Forum Moderators: phranque
Doing a site search on "301" returns over 75,000 results. I spend half day reading feedback and different opinions but still try to conclude my query: I got two, newly purchased domain names example-long dot com and example-short dot com. The reason I purchased both is because one of the two (example-short) is the abbreviated version of the example-long. So my intention is to have example-short as the 'primary address'. I purchased example-long, because I want that if in case somebody types example-long, they redirected to example-short (transferring the PR as well!). Do I need to maintain 2 copies of the websites on each domain? Out of the 100s posts concerning 301s, which I think is the solution to this problem, I separated the most commonly referenced:
- 1 -------------------------------------
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.examplelong\.com
RewriteRule (.*) http://www.exampleshort.com/$1 [R=301,L]
- 2 -------------------------------------
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.examplelong\.com
RewriteRule ^(.*)$ http://www.exampleshort.com/$1 [R=301,L]
- 3 -------------------------------------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^exampleshort\.com
RewriteRule (.*) http://www.exampleshort.com/$1 [R=301,L]
- 4 -------------------------------------
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com¦192\.168\.127\.1 [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
-----------------------------------------
Now, looking at (3) I think it would transfer all non-www traffic towards www-enabled name (which I also want to do). For example, if I type google dot com on the Internet browser bar, it automatically turns to the www-enabled google dot com - which is what I want as well. Although the above pieces of code are very similar in performing similar actions, which one do you recommend? To sum up, I would like to know:
a) The code that will redirect any non-www as well as www-enabled requests from example-long, towards the www-enabled example-short.
b) Redirect any non-www of example-short towards the www-enabled of example-short.
Would it be possible for anyone wishing to reply by modifying the above code, rather than suggesting that the x rule is better the y... I can roughly see what the code attempts to do - it is the syntax I am seriously struggling with and from that I heard, any slightest error in that code could cause all sorts of trouble.
I hope I didn't sound too much confusing.
Thank you in advance.
[edited by: jdMorgan at 10:42 pm (utc) on April 5, 2006]
[edit reason] Formatting [/edit]
# Turn on the rewrite engine
RewriteEngine on
# If hostname is non=blank
RewriteCond %{HTTP_HOST} .
# and hostname is not desired canonical domain
RewriteCond %{HTTP_HOST} !^www\.exampleshort\.com
# redirect to canconical domain
RewriteRule (.*) http://www.exampleshort.com/$1 [R=301,L]
Be eaware that a space is required between "}" and "!" in RewriteConds -- posting on this forum removes that space.
Please don't use any of this code unless you understand exactly how it works. These are server configuration directives, and should not be used lightly.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
I will read this information and then have a go and contact my host to see which of these commands are supported. I need to describe them what I want to do, because it might as well be possible to do this via the web-based control panel of the server config setting, thus not messing with code at all(!). Regardless of that it is always good to know by editing the .htaccess file.
I will post back any interesting findings (in any!).
Regards.