views:

57

answers:

6

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?

A: 

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

<?php

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

?>
chigley
Why `die();` right before the script exits anyways?
meagar
Yes, in the end, death comes naturally.
Archimedix
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.
chigley
A: 

put an index.php in / containing:

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

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

Ross
+1  A: 

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.

+3  A: 

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] 
alexn
We don't know what web server he's on.
@user257493 : Apache 2.2.14 and PHP 5.3.1
Yassir
@user257493 I assumed that it was apache since he mentioned PHP and nothing else.
alexn
+1  A: 

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

Redirect 301 / /public/
naivists