Forum Moderators: phranque
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.co.uk [NC]
RewriteRule (.*) [mydomain.com...] [R=301,L]
However I also need it so that the non-www version (mydomain.com) points to www.mydomain.com
I have several Search engine URL redirects going on also which I need to not break which are:
# SEO URLs rewrite BOF
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)-design-p(.*).php website-design.php?portid=$2
RewriteRule ^web-portfolio-(.*)-(.*).php web-portfolio.php?itemid=$1
I assume I am ok to leave those names in as they are since it doesnt mention my domain anywhere.
I am not a htaccess expert and I am getting a bit stuck with trying to get it all working without loops breaking things :S
Any help would be really appreciated.
Callena
# Options +FollowSymLinks
RewriteEngine on
# RewriteBase /
#
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# SEO URLs rewrite BOF
RewriteRule ^([^-]+)-design-p([^.]+)\.php website-design.php?portid=$2 [L]
RewriteRule ^web-portfolio-([^-]+)-([^.]+)\.php web-portfolio.php?itemid=$1 [L]
I also show some improvements to your SEF rules. These should result in a noticeable performance improvement on your server if you have a very busy site. However, the changes shown here assume that, for example, your "design-p" URLs have only one "word" before the first hyphen, and only one "word" between "design-p" and ".php", and that the first "word" does not contain a hyphen and that the second word cannot contain a period. If this is not true, then the improved patterns won't work as-is, but would need further adjustment.
The main purpose is to avoid the use of multiple ".*" subpatterns in your patterns. These are highly-inefficient, as they can result in dozens, hundreds, or even thousands of "back-off-and-retry" matching attempts. Use of specific negative-match patterns as shown allows the matching to proceed in a single left-to-right pass.
Note that literal periods in regex patterns should be escaped as shown, and that all rules should end with [L] unless you have a very good reason to leave it off.
Only one "RewriteEngine on" directive is needed. "RewriteBase /" is generally not needed, as it is the default setting.
Jim
[edit] Corrected as noted below. [/edit]
[edited by: jdMorgan at 2:30 pm (utc) on June 17, 2009]
Thanks for the really useful reply - sorry I changed my url to the mydomain (which obviously isnt my website :D), I'll make sure I use example.com in the future :)
I tried replacing my whole htaccess with your suggestion and I get a redirect loop. Unfortunately my knowledge of htaccess has meant most of what I've done has been taking other peoples examples of what they have done to make things work and tweaking them to fit my need at the time.
I seem to come across the redirect loop whatever I try and do. I am not sure if its because I am trying to get both .co.uk and .com to show the .com with www.
I am just trying to avoid having duplicate content penalties. The .com has been my main domain since we launched, but a few people have linked to us using the .co.uk over the years and I cannot get these links changed - and as a result we are appearing for some pages in google with the .co.uk
Maybe it doesnt matter if people type in just example.com instead of www.example.com but I would prefer to only have www.example.com ranking in the search engines incase we end up having some links without the www!
This seems to work fine:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co.uk [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# SEO URLs rewrite BOF
RewriteRule ^([^-]+)-design-p([^.]+)\.php website-design.php?portid=$2 [L]
RewriteRule ^web-portfolio-([^-]+)-([^.]+)\.php web-portfolio.php?itemid=$1 [L]
# SEO URLs rewrite EOF
So it must be something in:
RewriteCond %{HTTP_HOST} !^www\example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Should I have the !^www as !^www. or in brackets do you think?
Many thanks again for your help - its great to have someone who actually understands this and is prepared to help :)
This is correct as shown, but you must make sure that the domain in the first line exactly matches the domain in the second line, except that the literal periods must be escaped in the RewriteCond pattern:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
If your site is accessible via HTTP/1.0 (i.e. if you're on an IP-based, not a name-based server) then one more tweak is needed:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Jim