Htaccess redirect all to one file (except from localhost)

.htaccessapache-2.2gatewaymod-rewrite

I want to redirect all traffic to a single .php gateway file, this file will do authentication and use ajax to pull the file that was requested.

So I need a .htaccess with flow like this:

if external request for any file
redirect (or rewrite) to /gateway.php

gateway.php would then need to be able to access the file.

This is the best ive come up with is:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
RewriteCond %{REQUEST_URI} !/gateway\.php$  
RewriteRule .* http://www.website.com/gateway.php?req=%{REQUEST_URI} [R=302,L]

This is working, but when the ajax on gateway.php tries to get the content from another page, it gets the content from gateway.php (because of the redirect). I need to add an exception somehow!

Any help would be greatly appreciated!

Best Answer

You don't need that RewriteBase in there, try this:

RewriteEngine on
RewriteRule ^/gateway\.php$ - [L]
RewriteCond %{REMOTE_HOST} ^127\.0\.0\.1
RewriteRule ^(.*)$ - [L]
RewriteRule ^(.*)$ http://www.website.com/gateway.php?req=%{REQUEST_URI} [R=302,L]

First you explicitly say that if gateway.php is requested, stop. Then you say if it's from localhost just do the URL, ELSE do the redirect. You may be subject to some sort of internal redirect mechanism, you might have to set RewriteLog and RewriteLogLevel up right quick to debug the actions Apache is taking.

Related Topic