.htaccess redirect on Apache from non-www to www version - apache

I'm trying to redirect my website about dentists in London from the non-www to the www version and I'm using the following code on the .htaccess of my server, but it doesn't work. What do I miss?
Thank you
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.topdentists.co.uk [nc]
RewriteRule ^(.*)$ http://www.topdentists.co.uk/$1 [r=301,nc]

Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This is more general and will work for any site, not just the one you are currently working on. Taken from: Dense13.com (explanation of the code is in the second comment, underneath the article)
The reason why I think your code didn't work is because the RewriteCond uses a regular expression, but you didn't escape the dot (which is a regex-character).

To properly handle any potential https requests, as well as any strange characters, something like this is usually recommended:
# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

.htcaccess file assistance needed for canonicalization

My website currently has two version to google, https://www.example.com and a https://example.com This is apparently not good for SEO as its leaking all my seo 'juice' away. I have already told google to use just the www version around 5 weeks ago but come results on google still show the non-www version. I was wondering if anyone could help with my .htaccess to get the redirect right to just www.
The htcaccess file is copied below:
RewriteEngine on
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(blog)
RewriteCond %{HTTP_HOST} ^`.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.`.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
RewriteRule ^(.+)$ /index.php [L]
For a simple redirect you can use:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you want to also force HTTPS:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: This block goes above the www. rewrite rules to make sure both rules take hold

.htaccess file making new domain redirect to my original domain

To give a little background that may help understand this. I only had one domain on my host's server initially. I just added a new domain and it was redirecting to my first site.
I had to create an .htaccess file redirect my first site so that it would have my SSL on every page and so that it took the www. away from the sites name. The www. was causing issues with sessions when a customer would login.
So my code was like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
The tech support of my host company said that this line was making my new site redirect to my old site:
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
How can I make my code work so that is still has the exact same functionality of what it was doing and not interfere with my new domain and make it re-route to my old site?
Why not just exclude the new site, if I am understanding correctly?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?newsite\.com [NC]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com [NC]
RewriteRule ^(.*)$ https://oldsite.com/$1 [R=301,NE,L]
RewriteCond %{HTTPS} ^off
RewriteCond %{HTTP_HOST} !^(www\.)?newsite\.com [NC]
RewriteRule ^(.*) https://oldsite.com/$1 [R=301,NE,L]
Alternatively maybe you can just tell it to not do anything based on the host. Try putting this at the top of your rules.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com [NC]
RewriteRule ^ - [L]
Note I haven't tested this.

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

Folder to subdomain when autoappending www

We have a .htaccess-file like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mysite\.no [NC]
RewriteRule (.*) http://www.mysite.no/$1 [R=301,L]
Which autoappend www in front of the URL if it's missing. In addition we have to expand our site to a mobile version that we'd like to call m.mysite.no.
How do you do this and make the mod_rewrite ignore the www-rule? Unfortunately, this site is live, so we can't do much testing and we have little experience on the field.
Change:
RewriteCond %{HTTP_HOST} !^www\.mysite\.no [NC]
To:
RewriteCond %{HTTP_HOST} !^(www|m)\.mysite\.no [NC]

Forcing WWW with httpd.conf rather than .htaccess

I've read up on many Force WWW. tricks using htaccess. (As Below)
Rewritecond %{HTTP_HOST} !^www\.domain\.com
RewriteRule (.*) http://www.domina.com/$1 [R=301,L]
I'm wondering if there is a method to force WWW using httpd.config across the entire site.
The htaccess works a treat, but I have many subdirectories, most of them include there own .htacess files, which breaks the force www in that particular subdirectory.
So I was thinking, is there a way to force this "www." into httpd.config, across the entire site without editing .htaccess?
Thanks.
This should do it:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And to maintain HTTPS too:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This works for me
# direct all variation of FQDN to www.epoch.co.il
RewriteEngine on
#RewriteLogLevel 3
#RewriteLog "/var/log/httpd/epoch/www/epoch-rewrite_log"
RewriteCond %{HTTP_HOST} !^www\.epoch\.co\.il [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.epoch.co.il/$1 [L,R]