Forum Moderators: phranque

Message Too Old, No Replies

canonical mod rewrite in .htaccess for php site

sub pages are not redirecting to www

         

companytube

10:04 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Looking for some help with regards to the coding in the .htaccess file for redirecting non www urls to the corresponding www. version. The http version is currently redirecting to the www version for the index page, but not any sub pages. Any thoughts? Thank you.

current code:


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

[edited by: jdMorgan at 10:19 pm (utc) on Sep. 10, 2009]
[edit reason] example.com [/edit]

jdMorgan

10:31 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what you mean by a "sub page" -- They're either "pages in this directory" or "pages in a subdirectory of this directory," but "pages" either way.

If you mean that pages in subdirectories below the directory in which this code resides are not being redirected, and if there *is not* another .htaccess file in that subdirectory, then you might try adding a directive to your code (and cleaning up a few other items as well).

 
Options +FollowSymlinks
RewriteOptions inherit
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Also, if you *do not* use any subdomains other than "www", then the following code will be more comprehensive in redirecting non-canonical domain variations:
 
Options +FollowSymlinks
RewriteOptions inherit
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This latter example requires an exact match on "www.example.com" -- All variations of subdomain, FQDN, appended port numbers, and case variation will be redirected. The allowance for a blank hostname is to prevent HTTP/1.0 clients from causing an 'infinite loop' -- HTTP/1.0 clients do not send a Host header, so you don't want to redirect them or they'll come right back again with a blank hostname, causing a 'never-ending' redirection loop (Actually, the client or the server will give up after awhile, after this error has made a thorough mess in your log files).

Jim

companytube

10:32 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Also came across this post in another forum, however not sure if this will fix the issue:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# RewriteCond %{HTTP_HOST} !^www\.examplesite\.com$ [NC]
# RewriteRule ^(.*)$ [examplesite.com...]
%{REQUEST_URI} [L,R=301]
RewriteRule .* index.php/$0 [PT,L]
</IfModule>

companytube

10:47 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Thanks for the quick post Jim. Implemented this code:

Options +FollowSymlinks
RewriteOptions inherit
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

same issue as before...urls for sub folders without the www do not automatically redirect to the www version...any other thoughts?

jdMorgan

11:04 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you completely flush (delete) your browser cache before testing?

Do the sub-folders (and the requested objects in them) physically exist?

Do you have MultiViews enabled? Does your site require them (i.e. does it use content-negotiation)?

Are there any rules in other .htaccess files in directories above or below this one? Or in the server config files?

(Basically, something is interfering with the rule, which is perfectly-fine -- unlike that invalid/mis-coded mess you found elsewhere).

Jim

companytube

1:05 am on Sep 11, 2009 (gmt 0)

10+ Year Member



Thanks again for the indepth reply. Appears to be working with most of the subfolders...will see if some of the items you mentioned are causing the problem, or if it just a matter of the folders being virtual. Thanks again.

jdMorgan

2:37 am on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



folders being virtual

Try disabling MultiViews first, then:


Options +FollowSymlinks -MultiViews

That's a very likely culprit when the requested URL-path does not resolve to an existing server resource (see mod_negotiation if you want all the details).

Jim