We recently transferred a static website using SSL (e.g. https://www.mywebsite.com) to wordpress. It was decided that we would no longer be serving the pages via https, but did not want to lose any traffic or ranking in google.
WordPress thankfully allows users to setup permalinks (permanent urls to pages / posts etc) so the urls on the site can be search engine friendly. We typically use /%postname%/ structure which generates the following in an .htaccess file
RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
This is great as all the pages now have nice looking links but doesn’t solve the issue where old URLs need to point to the new ones. i.e. https://www.mywebsite.com/example_page.php to http://www.mywebsite.com/example-page/
For this we applied a plugin called 301 redirect which simply gives you a UI to redirect your pages without having having to add the URLs to your .htaccess file which can be a pain to manage and a slight typo could potentially disable access to the site altogether! (There are many other similar 301 redirect plugins out there, use whatever works for you)
Although this plugin does what it says on the tin, it wasn’t very helpful in redirecting https traffic to http, infact the redirects wouldn’t even show the wordpress 404 error.
The fix that needed to be applied was within the .htaccess file, and was quite simple:
RewriteEngine On RewriteCond %{SERVER_PORT} ^443$ RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L] RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
2 lines were added just below the RewriteEngine On
statement, this checks for https (SSL) traffic before the redirect rules even kick in.
Problem solved! Site traffic was being redirected and all the new pages are successfully replaced in googles index without any issues.
Things to keep in mind:
- An SSL certificate is still required
- Browsing to the permalinks page after applying this fix will replace the .htaccess file with the original redirect code deleting the changes.
- Applying plugins that use the .htaccess file could alter the newly added code as well
Obviously keep an eye on your webmaster tools and pick up any 404 errors from within the sitemap.
Happy migrating!
Thanks so much! 🙂
Need help on how to redirect all the https old links(whenever user opens the website using https) to the http in wordpress multi site
You say that “An SSL certificate is still required”?
Yes, otherwise you will get https errors and the redirect won’t work.
I’m using this but browsers are still getting stuck. I get an error about too many redirects.
Thanks! your solution worked.
hi,
this is what im looking for, thankyou for the sharring and know my vps is running good
Thank you for sharing this code. This actually made the trick. Good thing I came across this easy to follow codes about redirection.
Wow! it did the trick!
Thanks so much!
Hi,
I had a similar situation: I went back from a full-htts site to a http with https back-end. I’d like to ask you how can I edit this code so that I can still use https in wp-login/wp-admin?
At the moment, with that code I’m always redirected in http, even when trying to login the back end.
Sorry for bothering you but I’m not very .htaccess-friendly.
Thanks!
@mastro, this is untested but you could try this:
replace
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
with
RewriteCond %{SERVER_PORT} !^80$
RewriteRule !^wp-(admin|login|register)(.*) - [C]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [R=301,L]
More details here: http://codex.wordpress.org/Administration_Over_SSL
Let us know how you get on?