1

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!

1
  • actually, you can write the second rule as 'RewriteRule ^page1\.html$ page2.html [R=301]'; see the documentation for RewriteBase Commented Dec 4, 2008 at 0:34

2 Answers 2

4

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

RewriteOptions inherit

cf. the documentation

Sign up to request clarification or add additional context in comments.

Comments

2

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.