Redirect http:// to https:// and more


#1

Hi,

I would like to redirect my domain from http to https.

But when thinking about duplicatie content, a common issue is the trailing slash. For this reason I would like to implement a 301 redirection to the page with the trailing slash as a first step. The second step should be the redirection from http to https regardless wether the URL contains a www or not.

So here is what I implemented so far:

RewriteEngine On

--------------------------------------------------------------

Redirect 301 “without Trailing Slash” to “with Trailing Slash”

--------------------------------------------------------------

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.)/$
RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|jpeg|css|js)$ [NC]
RewriteRule ^(.
)$ http://%{HTTP_HOST}/$1/ [L,R=301]

--------------------------

Redirect http 301 to https

--------------------------

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

But when I check my redirection with a Redirect Checker Tool like http://www.redirect-checker.org I get the result:

Too many redirects. Please try to reduce your number of redirects… Actually you use 19 Redirects…

In addition, the redirection from https:// to https://www does not work.

What am I doing wrong? Thanks in advance!

Regards
Peter


Announcing the Top Users for the month of December (2017)
#2

Hi,

I am from Customer Success team of Cloudways. We have added two separate rewrite rule to redirect from non-www to www and HTTP to https. It will always open https with www regardless of which combination you try.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^prologcyclingwear.com [NC]
RewriteRule ^(.)$ http://www.prologcyclingwear.com/$1 [L,R=301,NC]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.
)$ https://%{HTTP_HOST}/$1 [R=301,L]

Your trailing slash rules are working as it was before.

Regards


#3

Thank you, it worked perfectly for me too.


#4

Hi,

Many thanks! Now it’s working perfectly!

Regards


#5

Thank you!
Worked for me as well.


#6

I am having problems with this same issue. I used the text posted by Faizan khan above. Additionally, I need a Rewrite rule for “pretty URLs” and one to redirect from the root directory to the content directory. Here is the text of my .htaccess file:

Options All -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wikilawschool.net [NC]
RewriteRule ^(.*)$ http://www.wikilawschool.net/$1 [L,R=301,NC]
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Pretty URLs
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]
</IfModule>

The problems I am experiencing are:

  • When I direct the browser to https://wikilawschool.net it does not redirect to add the www and as a result the browser returns the message Your connection is not secure.
  • When I type the domain name into a redirection checker such as http://redirectdetective.com/ or http://redirectcheck.com/index.php, they return a large number of redirects, indicating a loop is happening.

Any help is greatly appreciated!


#7

Hello,

@HandwovenBox

Before answering, I would like to confirm one simple thing -

  • Do you want to keep (HTTPS + www) for all HTTP requests?

#8

Yes, that is what I would like to do.


#9

Try this one

# Force HTTPS + www in one go
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^wikilawschool\.net [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.wikilawschool.net/$1 [R=301,L]

Note: This rule should be placed in the top of .htaccess file before any other rewrite rule.
Edit: Corrected by adding missing OR flag. Credit: Jeff


#10

Thank you! One question: Should the third line have an [OR] flag like:
RewriteCond %{HTTP_HOST} ^wikilawschool\.net [NC,OR]

Because if I understand it, that line checks to see if www is missing (which by itself we want to trigger the RewriteRule), and then the next line checks to see if https was not used (which by itself should also trigger the RewriteRule).

Edit: added the not

Edit2: removed the space in the flag


#11

You are right. Thanks.

Once done, please make sure to check HTTP header response for all URLs variation


#12

Great, it’s working perfectly. At first I included a space in the flag [NC,OR] and it broke stuff–so don’t do what I did! Once I removed the space everything is good.

On an interesting note: at first after I uploaded the updated .htaccess file, it still was not redirecting https://wikilawschool.net properly–everything else was except that. Whenever I entered that address, I got a warning that this domain was not part of the SSL so the browser wouldn’t go there without the www.

I decided to just accept that and make sure to include the domain wikilawschool.net on the SSL certificate. The minute I regenerated the SSL to include the both domains, it started properly redirecting https://wikilawschool.net to https://www.wikilawschool.net. Coincidence? Or would the browser’s security prevent the redirect from happening?

Anyway, huge thank you to solving the problem for me!


#13

Glad to know, it successfully resolved.

For enabling HTTPS on www and non-www even for same domain; we install SSL for both variations.

#####Source: KB

I hope, after regenerating SSL certificate, your website is working fine. :slight_smile:

Thanks


#14

The https redirect is working fine for me, but I’m not able to pass the query string variables to https. Here’s what I’ve been trying, but it’s not working.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)$
RewriteCond %{HTTP_HOST} ^content.ad [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.
)$ https://www.content.ad/$1?%1 [L]


#15

Try this way…


#16

No luck. The goal here is to get urls like the one below to forward to https. When I changed to the syntax you suggested above, the domain redirected to https, but the query string parameters were not passed.

content.ad/?utm_source=affiliaxe


#17

Can you please mention domain name (If privacy is concerned, you could inbox also).

Thanks


#18

The domain is content.ad


#19

@systems
I can see, It’s redirecting with HTTP code 302, instead of 301 and it is missing query string value. Probably, there is something wrong with your .htaccess rule.

Solution: Keep this rule at the top of WordPress default rewrite rule. And, don’t use any other things for similar purposes. After then, make sure to Purge Varnish Cache.

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

#20

Use this tool, it works great.

https://varvy.com/tools/redirects/