Forum Moderators: phranque

Message Too Old, No Replies

Rewrite not working for subdirectories

         

makenoiz

10:30 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



Have the following


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]


This is in my .htaccess file in my public_html/ dir . It works well except when it hits subdirectories like
http://domain.com/subdiretory/file.html
, the browser URL does not change to
http://www.domain.com/subdiretory/file.html
.

The browser URL will change if the file is not a subdirectory file.

I need the browser URL to change to include the www for SEO purposes for every file including sub directory files. Can anyone help me?

Thank you
Van

[edited by: jdMorgan at 1:00 am (utc) on Feb 12, 2010]
[edit reason] de-linked [/edit]

jdMorgan

1:00 am on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code should not work at all in .htaccess, because the URL-paths tested by RewriteCond in .htaccess (or within <Directory> sections in server config files) do not start with a slash.

Also, this code does not appear to redirect to "www" even if it does get invoked. It appears to redirect to the same domain as requested -- which would cause an 'infinite' redirection loop.

I'd suggest:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^([a-z\-]+\.([a-z]{3,6}|co\.[a-z]{2}))\.?(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([a-z\-]+\.([a-z]{3,6}|co\.[a-z]{2}))(\.|\.?:[0-9]+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

The second rewritecond takes care of FQDN-format "www" domains (trailing period) and appended port numbers, e.g. the valid but non-canonical "www.example.com.:80"

However, that leaves the question, "If your original code wasn't redirecting to 'www', then what was?" If you've previously tried to do this redirect using your "control panel," I suggest that you remove that setting first, before trying to use .htaccess code to do it.

As always, completely flush (delete) your browser cache before testing any new server-side code.

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 2:55 am (utc) on Feb 12, 2010]

makenoiz

2:11 am on Feb 12, 2010 (gmt 0)

10+ Year Member



I get an internal server error:

This is the code I placed. This is the only code in the .htaccess file. It should be identical to what you have above as I cut and pasted but just double checking.... thanks

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^([a-z\-]+\.([a-z]{3,6}|co\.[a-z]{2}))\.?(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([a-z\-]+\.([a-z]{3,6}|co\.[a-z]{2}))(\.|\.?(:[0-9]+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L

[edited by: jdMorgan at 2:21 pm (utc) on Feb 12, 2010]
[edit reason] de-linked [/edit]

jdMorgan

2:58 am on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I typed too fast and had an extra "(" in the second RewriteCond. I corrected the code above to prevent the error spreading.

If you get a server error, look at your server error log -- It usually contains good info on errors, and may be faster than waiting around here for an answer.

Jim

makenoiz

3:52 am on Feb 12, 2010 (gmt 0)

10+ Year Member



Well, no more internal error however the sub directories URLs do not change:

http://example.com/landing-pages/test.html


should redirect to
http://www.example.com/landing-pages/test.html


but it does not.... it works correctly on the root though

http://example.com
does redirect to
http://www.example.com


By the way... you rock. I appreciate your attention.

Van

[edited by: jdMorgan at 2:22 pm (utc) on Feb 12, 2010]
[edit reason] example.com [/edit]

makenoiz

6:27 am on Feb 12, 2010 (gmt 0)

10+ Year Member



Here is something that may show the answer to a non noob: One works or the other.. cant get both to work at the same time. Either the root files will redirect correctly or the subdirectory files but never both at the same time.

works for root files only
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]


works for sub dir files only
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

[edited by: jdMorgan at 2:24 pm (utc) on Feb 12, 2010]
[edit reason] example.com [/edit]

makenoiz

7:58 am on Feb 12, 2010 (gmt 0)

10+ Year Member



OK Ive simplified it even more:

These two lines look like equivalent statements but one affects the redirection of root files only and the other affects the redirection of subdirectory files only. Can you tell why? And they will not work together if both are uncommented at the same time. If you see why, please enlighten me.

# this rule works for root docs only
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]


# This rule works for sub dir docs only
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

[edited by: jdMorgan at 2:24 pm (utc) on Feb 12, 2010]
[edit reason] De-linked. example.com. [/edit]

jdMorgan

2:27 pm on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



However, that leaves the question, "If your original code wasn't redirecting to 'www', then what was?" If you've previously tried to do this redirect using your "control panel," I suggest that you remove that setting first, before trying to use .htaccess code to do it.


You've got something interfering with your code. There's nothing to be done until you find out what that is, and remove it or modify it to eliminate the interference. This might be additional code in a file above the directory where the code we're talking about resides, or in the subdirectory/subdirectories you're having trouble with. It could be code in a server config file, added by your host, or by yourself using your 'control panel'. It could be mod_rewrite, mod_alias, mod_dir, mod_negotiation, mod_speling, and probably some other module(s) I've forgotten.

The code posted in this thread is dirt-simple, and in most cases it should work, regardless of subdirectory-level -- the exception being the first rule in the immediately-previous post, which leaves "%1" undefined.

And again, any RewriteRule with a pattern starting with a slash should never work in .htaccess on a properly-configured server, because RewriteRules in .htaccess cannot "see" a leading slash on the requested URL-path. So if in fact that first rule in the previous post "works," then you've got a server misconfiguration.

The only thing I can think of to try is to disable MultiViews (unless you use them). Change
 Options +FollowSymLinks 

to
 Options +FollowSymLinks -MultiViews 

and re-test the code I posted.

If that doesn't help, then I'm afraid you're going to have to ask your host for support. If they won't support you and give you the all-too-common "We don't support .htaccess questions" reply, then you will either have to pay them for support or find a better host.

When posting code, please use only "example.com" as the domain. Otherwise as you can see, the posts break due to auto-linking. I must correct all such posts, which means that any answer I might intend to provide will be delayed -- possibly by hours if I run out of time while bringing posts into compliance with our TOS and our Charter...

Jim

makenoiz

3:22 pm on Feb 12, 2010 (gmt 0)

10+ Year Member



Thank you. I will use example.com from now on. Sorry about that. And thanks for all the help!

You guys rock! I will do more investigation on my end today.

V