Page 1 of 1

Redirect Every Request to Page

Posted: Tue Jan 17, 2012 8:18 pm
by lenton
I want to every request for any file or directory on my server to be redirected to a specified file on the server.

The file I want everything to redirect to is: "/var/www/test.php".

I think this might be possible with htaccess files but I'm unsure, can anyone help?

Re: Redirect Every Request to Page

Posted: Wed Jan 18, 2012 6:46 am
by theserve
You should be able to do it with mod_rewrite.

i'm not 100% sure this will work but you can give it a go in your .htaccess

RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteRule ^.*$ test.php [NC,L]

Re: Redirect Every Request to Page

Posted: Wed Jan 18, 2012 2:10 pm
by twinedev
theserve wrote:You should be able to do it with mod_rewrite.

i'm not 100% sure this will work but you can give it a go in your .htaccess

RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteRule ^.*$ test.php [NC,L]
Additionally, if you want to know WHERE they tried to browse to:

Code: Select all

RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteRule ^(.*)$ /test.php?path=$1 [NC,L]
Now $_GET['path'] will be everything after the domain name un the URL (minus query string), and no worry, the visitor to the page will not see it. (ie, won't redirect to it.

HOWEVER, this will do EVERYTHING, including images, css, js... If you want to be able to still have those called, then do something like:

Code: Select all

RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test.php?path=$1 [NC,L]
Now it will take you to /test.php only when the request isn't an actual file or directory.

-Greg