Sign up ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

We have two main subdomains on our site: and Virtually all of our content is on, but there are a few standalone apps hosted on 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, 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 with 301s to the same path on I've tried a few different RewriteCond/RewriteRule setups, most recently

RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*)$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 got redirected to I've fixed the 500 error; that was happening because I hadn't enabled mod_rewrite. – michaelcgorman May 2 '12 at 16:09

1 Answer 1

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 the %{REQUEST_URI} variable will be equal /hello/kitten.php while %{REQUEST_FILENAME} will have full physical path on file system, e.g. /www/ (assuming that website root is /www/ 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 ^(.*)$$1 [R=302,L]


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*{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.