Forum Moderators: phranque
I have two domains; www.domain.co.uk and www.domain.com
Both domains point to the same webspace, and I've only been marketing the .com version.
I need the www.domain.co.uk domain to convert to the .com version seamlessly.
In addition to this, some of the pages on my site are dynamic, so the solution needs to be able to retain query strings.
Thanks for your help in advance. :-)
You dont need the whole url for the page you are redirecting from.
[httpd.apache.org...]
So, I could do something like this...
RedirectMatch (.*)domain\.co\.uk$ [domain.com.*$...]
?
That said if you wish to forward all requests for one page (co.uk) to the other (com) you will need a htaccess file in the same place as the first which redirects to the second so the regex is not needed.
RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST}!^123\.456\.7\.89
rewriterule ^(.*)$ [domain.com...] [R=permanent,L]
Is this correct syntax? Can anyone varify this for me please?
A few questions, if I may...
1) My server doesn't like Options +FollowSymLinks, is that a problem?
2) What's the difference between your version and the one I posted?
My One:
RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST}!^123\.456\.7\.89
rewriterule ^(.*)$ [domain.com...] [R=permanent,L]
Your One:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.co.uk [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ [yourdomain.com...] [R=301,L]
FollowSymLinks is required to enable mod_rewrite on some servers. If your redirects work without that directive, then you don't need to include it anyway.
There are several differences between the two rewrites posted here. The main difference is that I coded the second one to pass query strings - which you stated as a requirement. The first one won't pass query strings, unless there is something going on in your server that I don't know about.
A second difference is that the rewrite in the first post redirects requests to anything except the "preferred" domain and its IP address to the preferred domain. The second posted rewrite redirects only the .co.uk version of the domain (in either uppercase or lowercase letters) to the preferred .com domain.
Perhaps you didn't mean query string and don't use them:
[example.com...]
In that case, either version should work, with the above-noted behavioral difference.
If you'd like to learn more about the subjects of mod_rewrite and regular expressions, try this useful tutorial and follow the links to even more info: Introduction to mod_rewrite [webmasterworld.com]
HTH,
Jim
I've got your version up and it's working like a dream! ;-)
The php & cgi pages I have all use query strings (suprise!). Funnily enough, both versions seemed to work translating those, and the FollowSymLinks only resulted in a 500 error. Odd.
Anyhow, thanks for your help.
:-))))))
Quick question...
I still have the following version in my .htaccess file:
RewriteCond %{HTTP_HOST} ^www\.domain\.co.uk [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ [domain.com...] [R=301]
It works correctly if I terminante a url with a /, however, if I leave this off, the server redirects to [domain.co.uk...]
My site is in my profile if you want to test it out.
Any ideas and/or help would be greatly appreciated. :-)