Forum Moderators: phranque

Message Too Old, No Replies

.htaccess again: adding "www" to subdirectory pathnames

Trying to get htaccess to add www to subdirectory pathnames

         

Zig28

3:54 pm on Jan 4, 2009 (gmt 0)

10+ Year Member



Hi Everyone:
Very sorry, here's yet another .htaccess question! I would appreciate your help on this.

I'm using .htaccess to redirect "non-www" entries to www, like "mysite.com" to "http://www.mysite.com". I also wanted to block hot-linking of some file types in some of the subdirectories. Since I have multiple subdirectories, some with photos and some without, I have a root .htaccess file and an .htaccess file in some of the subdirectories.

The root .htaccess file on my server includes these lines:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) [mysite.com...] [R=301,L]

This redirects the main URL correctly, so if someone enters "mysite.com" they get redirected to "http://www.mysite.com.

But it does not seem to add the "www" to the incorrectly entered subdirectory URLs.

So if someone enters:

"mysite.com/cats/persian/persian-cats.html"

they get:

"http://mysite.com/cats/persian/persian-cats.html"

instead of

"http://www.mysite.com/cats/persian/persian-cats.html"

I've even tried placing an .htaccess file with just the redirect code above in the subdirectories, but it still does not redirect the "non-www" subdirectory entries to the "www" form.

So my question is, how can I get the "non-www" redirect to work for all of the longer subdirectory pathnames also? Is there a subdirectory or path wildcard or something I'm missing in the code above that would tell .htaccess to add the "www" in any pathname?

I've searched all over but can not find an answer, sorry if I missed it?

Thanks..!

jdMorgan

4:23 pm on Jan 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code is correct and should behave as you describe -- It should not care what the URL-path is, and should redirect *any* non-www request to www. As long as you placed your external redirect ( "[R=301]" ) rules ahead of your internal rewrites, you shouldn't be having a problem.

However, your server may not be configured so that child subdirectories inherit the mod_rewrite rules of their parents, or you may have other modules interfering.

I suggest disabling MultiViews if you're not using content negotiation, and adding


RewriteOptions Inherit

ahead of the RewriteEngine directive in your .htaccess file.

If this does not help, ask your host to turn off "UseCanonicalName" in the server config.

Jim

Zig28

4:30 pm on Jan 4, 2009 (gmt 0)

10+ Year Member



10-4, thanks for the quick reply! Will check this...

Zig28

5:25 pm on Jan 4, 2009 (gmt 0)

10+ Year Member



Well, still not working. the UseCanonicalName is off, that one's easy to check.

Not sure about the Multiviews though, here's the entry in my httpd.conf, I'm not sure if this is on or off (looks like it's on?) and what I should change to turn it off?:

<Directory "/usr/local/apache/icons">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all

</Directory>

Thanks...!

jdMorgan

5:46 pm on Jan 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you look up the "Options" directive in the Apache core documentation?

Jim

Zig28

7:14 pm on Jan 4, 2009 (gmt 0)

10+ Year Member



I looked - honest - but the documentation is clear as mud to me, otherwise there'd be far fewer htaccess questions on WebmasterWorld I'm thinking! ;-)

So the Options doc says to put a "-" sign to turn it off, just want to check, as I have limited experience, would the way I have it below turn off the ability to index and the MultiViews?

The documentation appears to say not to use only 1 "-" sign, so I figured I'd turn both off?

The only problem here is this is the only place in my httpd.conf file where the word "MultiViews" appears, but I'm not sure if this directory it's pointing to (/usr/local/apache/icons) will do the trick for me:

<Directory "/usr/local/apache/icons">
Options -Indexes -MultiViews
AllowOverride None
Order allow,deny
Allow from all

</Directory>

Thanks again for your help!

Zig28

7:19 pm on Jan 4, 2009 (gmt 0)

10+ Year Member



BTW, I also added "RewriteOptions Inherit" before my rewrite command in the .htaccess file just to see, but that didn't work either, so hopefully turning off the MultiViews in httpd.conf will help...

jdMorgan

7:28 pm on Jan 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That should work, as far as disabling MultiViews and directory index listing creation/display. But the "AllowOverride None" means that any Options you want to be able to use in .htaccess will have to be set in the server config. For example, you cannot use mod_rewrite if FollowSymLinks or SymLinksIfOwnerMatch is not set. So you'll need to set one of those in the httpd.conf file in order to use mod_rewrite at all, unless you change the AllowOverride settings to allow Options to be set in .htaccess files.

I encourage you to test things and see what they do, rather than waiting around here for answers which might take hours. It's faster and you may learn something. Also, I'm not aware of any directives that will actually cause the server to burst into flames (that feature won't be available until Apache 3.x Beta at the earliest), so just keep a backup of any changes you make, and you'll be fine... :)

Jim

Zig28

7:49 pm on Jan 4, 2009 (gmt 0)

10+ Year Member



Thanks - just for the record, I've been hacking at this for two days now, scouring the internet and trying things. I've tried working with the web host where my server is located, but they're clueless.

How anybody gets anything done with Apache servers is beyond me, considering the obscurity of the information. Definitely not written for someone who only has to dabble in it once and a while! ;-)

g1smd

8:18 pm on Jan 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's where this forum comes in... and the tens of thousands of posts over the last few years.