Are these url rewrite rules and conditions correct?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Basically, URL needs to be rewritten from non-www to www.
Thanks
I would probably use a more generic rule:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But this rule does the same.
Related
I'm trying to redirect three cases to https://www. They are:
http://
http://www
https://
I've been able to get the first two, but not the last one. Here's what I'm using for that:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]
You can use this rule to add www and turn on https in same rule:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
To capture value from %{HTTP_HOST} you need a condition that is not it OR condition.
I am routing all non-www requests to the www-domain by using this RewriteCond and RewriteRule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
But there is one certain subdomain that I want to exclude, e.g. blog.mydomain.tld. How do I have to modify my condition and rule in order to achieve this?
You can just add that subdomain in regex of your RewriteCond:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|blog)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
Did you try adding
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^blog\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
We are switching to HTTPS.
Our current .htaccess file has the following code to force non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now, for HTTPS this is my proposed addition:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
My question is - how do I merge - or combine - the non-www to www AND HTTPS? Or would I just keep the order as is above?
Thanks.
PS I have researched this but I don't seem to find any concise answer....
You can use that:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
Or without domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
I recommend you use the first. If indicate the domain name is not a problem.
We are now using OpenCart and domain aliasses in DirectAdmin.
We now have some rules in our htaccess for redirecting non www to www and non https to https.
But this rule is for all domains. We only want to use it for example-1.com
How can we do this? Htaccess we have tried all failled to do this.
This is the current htaccess we have
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
We only want this for example-1 and example 2 also. But now it always redirects to example-1
I understand you only try to redirect example-1 and example-2 and not other domains.
You can use that:
RewriteCond %{HTTP_HOST} (?:^|\.)(example-1\.com|example-2\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
Try:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]
I'm trying to make a general rewrite rule to redirect all domain.com requests to www.domain.com.
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+])$ [NC]
RewriteRule ^(.*)$ http://www.{HTTP_HOST}/$1 [R=301,L]
The problem is that this rewrite rule doesn't match anything. How can I change it? Thanks
I guess the problem is that there’s an additional ] in your RewriteCond’s pattern and that there is a % missing when referencing HTTP_HOST in RewriteRule’s substitution. So try this:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
And to cover Cags’ concerns a litte bit, you can also try this rule:
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]