0

I know similar questions have come up, though often without a working answer. I'm hoping to have better luck!

I have an .htaccess file in my root directory adding "www" to everything:

RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]

This generally works fine. I have a subfolder (/myquiz/) in which the old index.html file has been replaced with index.php. I know there are external links to /myquiz/index.html, so I want to make sure those redirect. Leaving index.html in place and trying to redirect from that led to some odd behavior, but adding an .htaccess in that directory works for that:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]

Trying to load index.html redirects to index.php as hoped for, and the WWW gets added if needed. But requesting mysite.org/myquiz/index.php directly does not add the WWW.

I tried adding "RewriteEngine inherit", but that resulted in calls getting redirected to my root folder instead. A great trick if I want to make a subfolder inaccessible, but not helping here. I also tried just adding the code from my root .htaccess into the beginning of my subfolder's .htaccess, but that worked no better.

Any ideas?

Community
  • 1
  • 1
hartshoj
  • 1,053
  • 7
  • 12

2 Answers2

0

You shouldn't need to add another htaccess file in the myquiz folder. This should work in the htaccess file in the root of the site. Remove the htaccess file in myquiz and try this.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]

Also I wouldn't use %{SERVER_NAME} unless your are sure the name is set properly in the config file. Then it can be more reliable than HTTP_HOST, otherwise I would instead use %{HTTP_HOST}.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • This worked. Thanks! Just to be clear -- you don't think there's any advantage to having redirects that are only relevant for a local folder be in that folder? Right now the root folder .htaccess is pretty simple, but I could imagine it becoming very long. – hartshoj Feb 18 '14 at 19:09
  • @user3291354 I think it would be better to have 1 because apache has to scan and read every directory for an htaccess file. So it 'might' help with performance. I prefer to have all rules in the root if possible because I can easily make changes since all the rules are in the same spot. – Panama Jack Feb 18 '14 at 21:26
0

I think inherit would work if you add an L flag to the rule that you have in your myquiz folder:

RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]

So that it redirects first, then the inherited rule (the www) gets applied after.

You could also just put both rules in the same file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]

RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I tried using inherit with the [L] flag as suggested above. That resulted in being redirected to the root folder. – hartshoj Feb 18 '14 at 19:01
  • As far as the second option, trying "www.mysite.org/myquiz/index.html" results in no redirection and a 404 (cause the file doesn't exist). "mysite.org/myquiz/index.php" gets redirected to "mysite.org/index.php" and a 404 (again, no such file). – hartshoj Feb 18 '14 at 19:05