0

I have a PHP web site that has two directories: An application directory and a public directory.

The problem is that the user has to go to www.domain.com/public to access the site but I need the user who asks for www.domain.com/ to be redirected to www.domain.com/public

So my question is what is the best way to do this?

0

6 Answers 6

3

I would do this with a .htaccess rewrite rule. This ensures that the user is always redirected, even if index.php is not requested. Something like this should work for you:

RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.*)$ public/$1 [L] 
Sign up to request clarification or add additional context in comments.

3 Comments

We don't know what web server he's on.
@user257493 : Apache 2.2.14 and PHP 5.3.1
@user257493 I assumed that it was apache since he mentioned PHP and nothing else.
2

multiple ways - .htaccess, or a simple

<?php header('Location:http://www.domain.com/public'); ?>

would do the trick if you don't need to access anything directly from domain.com explicitly

Comments

1

You should deal with this through your web server (IIS/Apache/otherwise), as it is much better suited and appropriate for this sort of task. SO has plenty of answers on URI redirection for various webservers.

...Also, if you're keeping people out of your "application directory" (which you seem to indicate is your web root), you should really re-design that if your intent is to keep people out of that folder: it's a security risk.

Comments

1

You can solve it in a .htaccess file as well. Just create a "Redirect" rule:

Redirect 301 / /public/

Comments

0

You could do a redirect with PHP. Inside your index.php in your main document root:

<?php

header('Location: public');
die();

?>

2 Comments

Yes, in the end, death comes naturally.
Valid point. It's just a course of habit, as it's good practice to call die() if there's more code below a conditional header(). Granted, in this case, there's absolutely no point for it.
0

put an index.php in / containing:

<?php
header('Location:http://domain.com/public');
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.