301 several (but not all) http to https - .htaccess

I have a site of about 1.000 static pages, and I would like to see how moving from http to https will effect ranking for, say, 50 pages before I move the entire site.
Do I have to use the first code example below or is it enough with the second one? Or is there a better way to do it?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page-1.htm/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page-2.htm/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page-3.htm/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# OR IS THIS ENOUGH:
Redirect 301 /page-1.htm https://www.example.com/page-1.htm
Redirect 301 /page-2.htm https://www.example.com/page-2.htm
Redirect 301 /page-3.htm https://www.example.com/page-3.htm

You can use a single rule to redirect multiple pages of this format page-n.htm
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^page-([0-9]+).htm$ https://%{HTTP_HOST}/page-$1.htm [NE,L,R=301]

If you explicitly want it in separate RewriteRule statements:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^page-1\.htm$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} off
RewriteRule ^page-2\.htm$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} off
RewriteRule ^page-3\.htm$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

Related

Force non-www/http to www/https only for some specific pages

I was wondering is it's possible to force some specific pages from non-www/http to www/https and keep some other ones with non-www/http.
Example
From non-www/http to www/https:
http://example.com to https://www.example.com
but these ones will remain non-www and http:
http://example.com/folder1/*
http://example.com/folder2/*
I have tried to add in the htaccess file this rule condition:
RewriteEngine On
# Enable HTTPS and WWW for homepage
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTPS} off
RewriteRule ^$ https://example.com/ [R=301,L]
# Disable HTTPS and WWW for all pages
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTPS} on
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Then it should force to https and www just the homepage and leave the other pages like /folder1/ and /folder2/ to htpp non-www.
But it seems not working well
Try these :
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^folder1.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^folder2.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^folder1.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^folder2.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^folder1.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
and so on...
Try this:
RewriteEngine On
RewriteCond %{HTTPS} off ## http requests only
RewriteRule ^folder1/ - [L] ## stop for folder1/
RewriteRule ^folder2/ - [L] ## stop for folder2/
## then redirect all other requests:
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [END,NE,R=permanent]
Similarly, you can you regex your skips:
RewriteRule ^folder(\d+)/ - [L] ## stop redirection for any folder[number]/
Or make this a negative RewriteCond keeping a single RewriteRule:
RewriteEngine On
RewriteCond %{HTTPS} off ## http requests only
RewriteCond %{REQUEST_URI} !^/folder(\d+)/ ## skip any folder[number]/
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [END,NE,R=permanent]
Etc... Full docs: https://httpd.apache.org/docs/2.4/rewrite/flags.html

301 redirect http to https with www

I have to redirect my domain from http to https. In my htaccess I have already.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This snippet redirect everything without "www" to "www".
When I change this to
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The result is:
http://www.example.com/folder/page.php
becomes
Location => https://www.example.com/folder/page.php
Fine!
https://example.com/folder/page.php
becomes
https://www.example.com/folder/page.php
Fine!
but:
http://example.com/folder/page.php
becomes
Location => https://example.com/folder/page.php
but it has to be
Location => https://www.example.com/folder/page.php
How is it possible to fix this?
I know all of this redirects:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
but I need only one redirection instead of two 301.
You can use the following rule
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{RERUEST_URI} [NE,L,R=301]
Clear your browser cache before testing this.
I have found a solution here:
https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https
The second answer by #w3dk is working.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger.
With letsencrypt its no problem to have this 2 certs.

Redirecting specific Domains to HTTPS

My domain www.abc.com is now redirecting to https://www.abc.co.uk
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
These code works exactly what I want. After that I created a subdomain. www.x.abc.co.uk
Now I want to redirect that to http://x.abc.co.uk (without https)
I used this
RewriteCond %{HTTP_HOST} ^x\.abc\.co\.uk [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But browser says it has redirect loop.
How can do that?
If these are two different htaccess files then give this a try.
RewriteEngine On
#redirect www.x.abc.co.uk without www to http
RewriteCond %{HTTP_HOST} ^www\.x\.abc\.co\.uk [NC]
RewriteRule ^(.*)$ http://x.abc.co.uk%{REQUEST_URI} [L,R=301]
I modified your original rule so you don't have two rewrite rules and still direct to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.abc.co.uk%{REQUEST_URI} [L,R=301]

301 redirect for entire site except for subdomains

I have setup a sitewide 301 redirect in my .htaccess as follows
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
But I wanted to exclude subdomains (support, blog) from being included in this redirect. So I add the following RewriteConditions
RewriteEngine On
RewriteCond %{HTTP_HOST} !=support.example.com
RewriteCond %{HTTP_HOST} !=blog.example.com
RewriteCond %{HTTPS} !=on
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This works totally fine. However I wonder if there is a better way to specify the exclusion for all subdomains (not WWW) in just 1 RewriteCond statement (instead of a separate RewriteCond for each subdomain)
I have a few more subdomains and plan to add some more. Would appreciate the help.
You can use this rule:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect to HTTP non-www to HTTPS www htaccess

I want to redirect from any direction to our site with HTTPS protocol, but some redirects it's not working. I want this:
http://www.site.co TO https://www.site.co
http://site.co TO https://www.site.co
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
The second rule it's not working. It going to another direction inside our site, and it isn't redirect to HTTPS site.
Try it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The only real difference here is that first we redirect from non-WWW to WWW then we check for HTTPS and redirect it.
If it does not work, try this one:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The answer by Prix works. To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name.
RewriteEngine On
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Sorry I don't have enough point to comment, but the rule of #prix will bring unneeded redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
You can try http://domain.com/ on GTMETRIX and you will get this message
"Avoid landing page redirects for the following chain of redirected URLs."
http://domain.com/
http://www.domain.com/
https://www.domain.com/
To prevent that, go to the first RewriteRule and add a "s" at the end of http. The new set of rule will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
This is http to https and non www to www redirection using .htaccess
If you want to redirect to https and www you can use this code
http to https redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
Non www to www redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
If you want to use booth functionality place the above all code respectively
In case you are using One.com as your webhost you should use the following code instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have an asp.net website and the host provider is not supporting IIS rewrite module i have tested all this methods and only this code gives woorank.com confirmation maybe useful for another .net developer :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will use for both www or non-www
If you try to open link with www then url redirect to https with www
Example : http://domain.com redirect to https://domain.com
or If you try to open link with non-www then url redirect to https with non-www
Example : http://www.domain.com redirect to https://www.domain.com
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Just put the following in the .htaccess file
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]
Try this methode:
# Redirect 301 all to https://www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
</IfModule>
Why sometimes you don't need code %{HTTP_HOST} and use your domain exact url with www for the last line, because usually the result will have two www like:
https://www.www.yourdomain.com
Btw if you use cloudflare, just create pagerules redirect 301 all to https and www version.
Create pagerules
Fill with
example.com/*
Choose setting forward url then choose 301 and fill with this
https://www.example.com/$1
Save it and all will be redirected to https and www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
For example https://example.com should be going to https://www.example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]
Note - you should change example.com to your own domain.

Resources