Mobile app version of vmapp.org
Login or Join
Carla537

: Replace 404 with 301 to other subdomain 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

@Carla537

Posted in: #301Redirect #Apache #HttpCode500

We have two main subdomains on our site: example.edu and php.example.edu. Virtually all of our content is on example.edu, but there are a few standalone apps hosted on php.example.edu. 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 example.edu. I've tried a few different RewriteCond/RewriteRule setups, most recently

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


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

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Carla537

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

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 ^(.*)$ www.example.edu/ [R=302,L]


or

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme