Webmasters Stack Exchange is a question and answer site for pro webmasters. 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

We have two main subdomains on our site: www.example.edu and php.example.edu. Virtually all of our content is on www.example.edu, but there are a few standalone apps hosted on php.example.edu. www.example.edu is maintained by non-technical users via a CMS that forces root-relative URLs in most cases. We would like to include departments' navigation links in their apps on php.example.edu, but those links obviously don't work as root-relative URLs. We would like to avoid server-side parsing/rewriting of sidenav files.

What we would like to do is to replace all 404s on php.example.edu with 301s to the same path on www.example.edu. I've tried a few different RewriteCond/RewriteRule setups, most recently

RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*) http://www.example.edu/$1 [R=302,L]

but none of them have worked yet, either ending in false-positives or 500 errors. Any suggestions?

share|improve this question
What do you mean by "false positives" .. and what web server (I assume it's Apache) says about 500 errors (check Apache's error log for EXACT error message). – LazyOne May 2 '12 at 16:00
"False positives" in the sense that php.example.edu/realdirectory/ got redirected to www.example.edu/realdirectory/. I've fixed the 500 error; that was happening because I hadn't enabled mod_rewrite. – michaelcgorman May 2 '12 at 16:09
up vote 1 down vote accepted

You have to use correct variables when writing the rule: %{REQUEST_URI} !-f makes no sense (unless requested URL = exact physical path on server -- 0.000001% chance to have such unusual setup).

You need to use %{REQUEST_FILENAME} instead of %{REQUEST_URI}. The difference is big: for php.example.edu/hello/kitten.php the %{REQUEST_URI} variable will be equal /hello/kitten.php while %{REQUEST_FILENAME} will have full physical path on file system, e.g. /www/php.example.com/httpdocs/hello/kitten.php (assuming that website root is /www/php.example.com/httpdocs). I highly doubt that you will have /hello/kitten.php file in place (relative to the file system root, of course, and not website).

Proper rule should look like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.example.edu/$1 [R=302,L]


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* http://www.example.edu%{REQUEST_URI} [R=302,L]

P.S. Don't forget to change 302 (Found) to 301 (Permanent redirect) after finishing testing (ensuring that rule is working fine).

share|improve this answer
Awesome. I knew it was something stupid like that. This does exactly what I need it to. Thanks, LazyOne! – michaelcgorman May 2 '12 at 17:59

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.