2

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?

2
  • 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, 2012 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. May 2, 2012 at 16:09

1 Answer 1

1

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]

or

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).

1
  • Awesome. I knew it was something stupid like that. This does exactly what I need it to. Thanks, LazyOne! May 2, 2012 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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