Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 .*{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!

share|improve this question

migrated from Jan 26 '10 at 2:43

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

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 ^(.*)${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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.