Forum Moderators: phranque

Message Too Old, No Replies

Complete newbie question

         

ilPadrino

11:47 pm on Feb 18, 2009 (gmt 0)

10+ Year Member



I am completely new to the world of .htaccess, so please bear with me. I am using a GoDaddy shared hosting plan. I am able to redirect my primary domain on the plan (call it mysite.com) to the www version with no problems using the following:

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

The files for this primary site are found in the root folder. However, when I attempt to do the same thing for any of the other sites, I run into problems. I assume it is because these other websites are inside separate folders within the root folder. For example, the files for myothersite.com would be in the folder "myothersite". Thus, when I alter the .htaccess file and try to visit myothersite.com, I get redirected to www.myothersite.com/myothersite/ rather than the desired www.myothersite.com. Can someone please show me what the following should look like so I can get myothersite.com to redirect to www.myothersite.com given my situation?

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

[edited by: jdMorgan at 1:37 am (utc) on Feb. 19, 2009]
[edit reason] de-linked [/edit]

jdMorgan

1:36 am on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure how your server is set up, but since you indicate that your redirect *does* work (if not perfectly), then this might do:

RewriteCond %{HTTP_HOST} ^myothersite\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.myothersite\.com(\.¦\.?:[0-9]+) [NC]
RewriteRule ^(myothersite/)?(.*)$ http://www.myothersite.com/[b]$2[/b] [R=301,L]

Note that I removed the end-anchor from the HTTP_HOST pattern and added code to fix problems with FQDN indicators and port numbers on the "www" hostname as well. You should also do this with your other hostname canonicalization rules.

If it's not clear, the following hostnames are perfectly-valid, but non-canonical:
myothersite.com
myothersite.com.
myothersite.com:80
myothersite.com.:80
www.myothersite.com.
www.myothersite.com:80
www.myothersite.com.:80

All seven will be corrected by the code above.

Important: Replace the broken pipe "¦" character in the second RewriteCond pattern with a solid pipe character before use; Posting in this forum modifies the pipe characters.

The basic problem is/was likely that GD is internally rewriting your myothersite.com domain to the filepath /myothersite *before* transferring control to your .htaccess file. Your code then does an external redirect, thus "exposing" the /myothersite internal file-path to the requesting HTTP clients (e.g. search robots and browsers).

This problem can occur even in a stand-alone .htaccess file, and this is why we recommend ordering your code with all external redirects first, in order from most-specific pattern (affects fewest URLs) to least-specific pattern (catch-alls affecting more URLs), followed by internal rewrites, again from most-specific to least-specific.

Jim

ilPadrino

2:23 am on Feb 19, 2009 (gmt 0)

10+ Year Member



Jim, thanks a million! I tried your suggestion and it works great. I really appreciate you taking the time to help and explaining what the problem was.