I have searched the web all over for a good generic .htaccess script for redirecting non-www to www, and currently i'm using this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine, but if i go to a subdomain www. will be added. Does anyone has a good working redirect .htaccess script?
Try this :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.]+)\.([a-z]{2,4})$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#Vince What if the requested url is something like:
http://www.abc.example.com
IMHO, I think with your method it would never be redirected to:
http://www.example.com
How about this?
# Rewrite domain
RewriteCond %{HTTP_HOST} !^www\.([a-z1-9\-]+)\.([a-z]+)$ [NC] [and]
RewriteCond %{HTTP_HOST} ([a-z1-9\-]+)\.([a-z]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
Also, you guys may find these references useful:
https://www.drupal.org/node/93603
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html
Related
Can you please help me how to redirect ONLY http://example.com to http://www.example.com?
I have some subdomains (http://sub1.example.com; http://sub2.example.com...) and I don't want them to be redirected also.
This is my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
With that, what I'm getting is that when I access to http://example.com it redirects correctly to http://www.example.com but when I access to http://sub1.example.com it redirects to http://www.www.sub1.example.com/sub1_folder/sub1_folder/
Thanks in advance
You can have a more generic regex pattern if you know your domain will be XXX.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Or even better:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[a-z]{2,4}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Case:
www.domain.com redirects to domain.com
www.domain.com/somecategory does not redirect to domain.com/somecategory.
The links on the page are relative, and this is causing problems with google as all www links are now duplicate content. Is there any way to fix this and force a non-www redirect on all WWW pages regardless of if it's root or not ?
htaccess:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
i've also tried
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I have changed your code to:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
And it seems to be working.
Without the slash in the rewrite rule, I was redirected to domain.compage instead of domain.com/page
I need to re-direct a url of a website of mine, if it hasn't the www in it, because of a script that dont work if the url isn't complete, how can i do this with a htacces file, I cant find an example on the web.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Something like this might work for you:
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http://www.%1%2 [R=301,L]
This is the code I currently have:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www2.domain.com/$1 [R=301,L]
and this redirects www.domain.com to www2.domain.com properly. I want the non-www to redirect to like ^www.(.*)$ [NC] does since it keeps urls intact and just let them sit on www2.domain.com/whatever instead.
I think you need this
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) http://www2.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www2.domain.com/$1 [R=301,L]
This works well, what if we only wanted to redirect the domain name to www2 for example we just want to redirect www.domain.com but not www.domain.com/folder?
How can I redirect all non-www links to www links? I have found solutions on the internet, but they only redirect the domain name.
How do I make this general:
http://example.com/testing should redirect to http://www.example.com/testing?
try something like this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.yourdomain.com/$1
If you want something generic that works for any domain, you can try something like:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule ^(.*)$ http://www.%1/$1