wordpress http to https redirect loop - wordpress

I am trying to force my site to load in HTTPS. I can create a redirect in PHP which works for PHP files, but I want all requests forced to HTTPS - JS, CSS, Images, etc.
Here's what I have in my .htaccess:
ErrorDocument 401 default
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I have tried various methods of this by placing the redirect in different locations and such, but everything seems to make this into a redirect loop. Any ideas what I could do to fix that?
Thank you.

Keep your http to https rules before internal WP rule:
ErrorDocument 401 default
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Make sure WP's home URL and site URL also have https in permalink settings.

Set a 301 permanent redirect which is more SEO friendly. Already tested:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect all traffic to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Wordpress rules
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Related

Code disappear sometimes from .htaccess

Hello guys i’m new and I have question. I have one website that have indexed https and http version in google search, and I added 301 redirect from HTTPS to HTTP in .htaccess. Sometimes, Randomly, 301 redirect disappeared from htaccess and I need to add it again.
This is my htaccess with 301 redirect:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And After some times, they change to this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So the redirect was completely removed, google index again https page and I need to add it again, why? I tried many hosting company but nothing changed.
I would not redirect vom HTTPS to HTTP, but the other way round. Google will give you a small ranking boost if you are using HTTPS (not to speak of it being the right thing to do to protect your users' privacy).
That being said: don't put anything between the WordPress markers, e.g.
# BEGIN WordPress
....
# END WordPress
WordPress will overwrite it the next time it writes to the .htaccess.
If you put it before that block, i.e.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You should be fine, and it should remain intact.
Thanks #janh, so this is correct?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# BEGIN WordPress
# END WordPress

How to move WordPress form HTTP to HTTPS

I have following code in my .htaccess file of a wp site
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have done all the thing need for a WordPress site to operate in HTTPS now my site is opening in both the format i.e in HTTP and HTTPS, but if the user type example.com it goes to http not in https so to solve this I add few more code to solve this issue
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
but after adding this my site in not opening it says "ERR_TOO_MANY_REDIRECTS"
How to solve this issue, I want the user to go directly to https
The problem is that you're not checking if https is being used, so it just constantly tells the browser to go to https://www.yoursite.com/$1.
You can use %{HTTPS} to check whether the request uses https.
RewriteEngine on
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]

https to http redirect not working

I have a Wordpress installation where some https URIs have been picked up by Google - the site does not have an SSL certificate and I don't want one. As a result I'm trying to redirect from https to http, but it's not working. All I'm seeing in Google Chrome is the "Your connection is not private" message.
I've put the redirect at the top of the htaccess file and I can't work out why the url isn't redirecting to http so that the message doesn't appear? I've also tried it on other devices and browsers. Any ideas why the redirect isn't working?
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
RemoveHandler .php
AddType application/php5-fcgi php
Action application/php5-fcgi /cgi-bin/php5fcgi.fcgi
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteBase /bevanevemy.co.uk/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#RewriteRule . /bevanevemy.co.uk/index.php [L]
</IfModule>
# END WordPress
You forgot your R,L modifiers ( Redirect, Last) this way, it will go to the url and stop processing rules.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RemoveHandler .php
AddType application/php5-fcgi php
Action application/php5-fcgi /cgi-bin/php5fcgi.fcgi
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteBase /bevanevemy.co.uk/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#RewriteRule . /bevanevemy.co.uk/index.php [L]
</IfModule>
# END WordPress
EDIT also, apache uses different file for ssl config. Maybe that is the problem
https://stackoverflow.com/a/8760123/3617531
Edit Your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

301 Redirect Error in .htaccess, shows "Bots Get Naked Version" instead of redirecting

I am looking to write 301 redirects for a site. I would like the redirect to go from http://www.ehlconsulting.com/our-services-basic-services/ to http://www.ehlconsulitng.com/services/.
My .htaccess file is as follow
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ehlconsulting.com
RewriteRule (.*) http://www.ehlconsulting.com/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Redirect 301 /our-services-basic-services/ http://www.ehlconsulting.com/services/
When you go to the old URL it doens't redirect. Instead it shows "Bots get the naked version"
You probably don't want to use Redirect and want to move the actual redirect above your wordpress rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ehlconsulting.com
RewriteRule (.*) http://www.ehlconsulting.com/$1 [R=301,L]
RewriteRule ^our-services-basic-services/(.*)$ http://www.ehlconsulting.com/services/$1 [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The "Bots get the naked version" thing doesn't have to do with your rules, it's probably WPEngine or something that's doing that by default, and you might be able to override that by using a ErrorDocument 404 in your htaccess file. The problem with using a Redirect along with wordpress' RewriteRule's is that they conflict with each other.

Redirect all pages to new domain

I want to redirect all the pages of my website to a new domain with a redirect. I've managed to get it working on domain level but not on the subpages.
So olddomain.com redirects to newdomain.com but olddomain.com/contact doesn't redirect to newdomain.com. It still shows the old domain.
I've used multiple rewrites rules but none of them seem to works. Any help is much appreciated.
This is the code that I'm using right now in my .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^racentegenkanker\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.racentegenkanker\.com$
RewriteRule ^(.*)$ "http\:\/\/againstcancer\.nl\/$1" [R=301,L]
For a full website redirection, you can use this (entire) .htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://againstcancer.nl/$1 [R=301,L]
EDIT
For two domains running side by side, with one redirecting the other :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} racentegenkanker\.com$
RewriteRule ^(.*)$ http://againstcancer.nl/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Resources