Hi David, i am trying to 301 redirect mysite.com to www.mysite.com. However none of the example i have come accross online seem to be working. Could there be a conflict with the SEF url code in .htaccess?
heres an example ive come accross
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L, R=301]
p.s. i dont have 'RewriteEngine on' twice.
cheers for your comments
Hi,
I just tried your code and got the same error. But I was able to make it work with this:
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]
Make sure that goes before the RewriteBase line...
Cheers,
David.
Hi David,
I'm trying to do something similar but want to redirect www to non-www.
Just been doing some checks in Google and I think I may have canonical problems. Generally, it looks like the URLs are indexed as follows:
Wordpress Install (Root) - URLs indexed as non-www
Price Tapestry Install (/shopping/) - URLs indexed as www
Ad-Hoc PHP pages in PT /shopping/ - URLs indexed as non-www
Additionally, in Google Webmaster Tools I've elected to have URLs indexed as non-www.
Perhaps it's not a huge issue but all the stuff I've read seems to suggest that having www and non-www is a bit of a school boy error from an SEO perspective!
Also, There's quite a few links pointing at this site now, mainly at the Wordpress Install and Ad-Hoc pages.
So, would it be a good idea to 301 redirect the Price Tapestry pages from www to non-www? What is the best way to do this?
Hope you can help.
Best,
Neil
Hi Neil,
Best done in .htaccess. Add the following lines immediately after the RewriteBase line but before any of the existing rules:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Cheers,
David.
Hi David,
Which .htaccess file does it go in: my root or /shopping/ ?
When I use this code in /shopping/ it kind of works but misses out a bit of the URL!
http://mysite.com/shopping/product/xyv.html (expected this)
http://mysite.com/product/xyv.html (but returns this)
Also, I tried this code in my root .htaccess file but it doesn't work.
BTW, the code structure in my root & /shopping/ .htaccess files looks like:
root .htaccess
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
AddType x-mapp-php5 .php
/shopping/ .htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /shopping/
RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^review/(.*).html$ reviews.php?q=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^merchant/$ merchants.php
RewriteRule ^merchant/(.*)/$ search.php?q=merchant:$1:&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^merchant/(.*)/(.*).html$ search.php?q=merchant:$1:&page=$2&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^category/$ categories.php
RewriteRule ^category/(.*)/$ search.php?q=category:$1:&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^category/(.*)/(.*).html$ search.php?q=category:$1:&page=$2&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^brand/$ brands.php
RewriteRule ^brand/(.*)/$ search.php?q=brand:$1:&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^brand/(.*)/(.*).html$ search.php?q=brand:$1:&page=$2&rewrite=1%{QUERY_STRING} [L]
Hi Neil,
It should go in /shopping/.htaccess - and as it looks like /shopping/ is implied it will need to be added to the rule, so have a go with:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/shopping/$1 [L,R=301]
Cheers,
David.
Worked perfectly :-)
Thanks again David for the great product & unrivalled support. Let me know if you ever need a testimonial.
Regards,
NEil
Hi David,
Hope you can help.
This has been puzzling me all morning and can't seem to find a fix for it. Slightly off topic wrt Price Tapestry but related to the above.
I've got quite a few 'site-wide' links from a high PR site pointing at my site. The destination URL used to point at my site included some tracking on it, so looks like this:
http://mysite.com/index.php?visitor_id=123xyz
In hindsight, I'd prefer if the link just pointed at
http://mysite.com
Rather than have to go back to the webmaster and have him change all the links I was hoping I could do a redirect in .htaccess
I've tried using this 301 redirect in my root .htaccess
Redirect 301 /index.php?visitor_id=123xyz http://www.mysite.com
but it doesn't work!!
Any ideas on how to do this?
Hi Neil,
Try something like:
RewriteRule ^index.php?visitor_id(.*)$ http://www.mysite.com/ [L,R=301]
(your version would probably have worked without the / infront of index.php as this is implied by RewriteBase - however adding the wildard (.*) will mean that you don't have to add multiple rules for different tracking codes...)
Cheers,
David.
Hi,
Does the redirect not happen, or does the site no longer work with the search engine friendly URLs?
I always do redirection in my Apache config so i'm not familiar with doing it this way - but is it necessary to "reset" RewriteCond after the redirection URL so that the subsequent rules are still processed...
Cheers,
David.