Redirect Every Request to Page

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Redirect Every Request to Page

Post 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?
User avatar
theserve
Forum Newbie
Posts: 24
Joined: Wed Jan 18, 2012 6:35 am
Location: London

Re: Redirect Every Request to Page

Post 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]
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Redirect Every Request to Page

Post 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
Post Reply