views:

1959

answers:

2

My dilema:

In .htaccess in my website's root:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

In .htaccess in the subdirectory /foo

RewriteEngine On
RewriteRule ^page1\.html$ /foo/page2.html [R=301]

In the first, I'm trying to ensure all requests include the beginning www. In the second, I'm redirecting requests for page1.html in the foo subdirectory to page2.html, also in that subdirectory.

In my browser, trying to visit:

http://www.example.com/foo/page2.html <== works, good

http://www.example.com/foo/page1.html <== redirects to http://www.example.com/foo/page2.html, good

http://example.com/foo/page1.html <== redirects to http://www.example.com/foo/page2.html, good

http://example.com/foo/page2.html <== no redirect occurs, bad

==> Should redirect to: http://www.example.com/foo/page2.html

Through experimenting, it would seem the redirect rules in the .htaccess file in the website's root only take effect for requests to pages in that subdirectory IF that subdirectory does not contain a .htaccess file, or it does and specifies a rewrite rule that does take effect for this particular request.

Can anyone see what I'm doing wrong? How can I get the rewrite rule that sticks the www. in if it's missing to fire for http://example.com/foo/page2.html?


Thank you hop, that worked!

For the record, I then had to change the rewrite rule in the file in the site's root to:

RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]

Which is just fine. Thanks!

+2  A: 

i'm not sure of the specifics as to why the rules in /.htaccess aren't applying to /foo/.htaccess. typically, .htaccess files will inherit rules up the directory structure, which can lead to all sorts of odd things. for your specific solution though, you could just place all relevant rules in your parent .htaccess file as such:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^foo/page1\.html$ /foo/page2.html [R=301]
Owen
+2  A: 

You are missing the following directive in foo/.htaccess:

RewriteOptions inherit

cf. the documentation

hop