Forum Moderators: phranque

Message Too Old, No Replies

From mysite.com to www.mysite.com

         

marciano

1:25 am on Feb 11, 2011 (gmt 0)

10+ Year Member



From an old post from JdMorgan I see

RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ [mysite.com...] [R=permanent,L]


to redirect mysite.com to www.mysite.com

My question is if is it necessary the slash before $1 as it may appears double in browsers

Is this better?
RewriteRule ^(.*)$ [mysite.com$1...] [R=permanent,L]

g1smd

9:04 am on Feb 11, 2011 (gmt 0)

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



The first code would be used in .htaccess where the leading slash is not present in $1.

The second code would be used in httpd.conf where the leading slash is present in $1.

marciano

11:59 am on Feb 11, 2011 (gmt 0)

10+ Year Member



Ah, okay, I am using it in httpd.conf
Thank you

jdMorgan

8:17 pm on Feb 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An alternative for use in server config files outside of any <Directory> containers is

RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.|:[0-9]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} [A-Z]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

When only a single hostname is to be used (i.e. no additional subdomains), then a simpler rule-set is:

RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

Both of these rule-sets take care of most non-canonical variants of the hostname, as well as showing an alternate way to handle the leading slash on the URL-path.

Note that in server config files, the use of separate RewriteConds is faster than using the "local-OR" inside a regular expression.

Jim