htaccess wordpress https rewrite rule - php

I am hoping that you can help me with the htaccess rewrite rule below.
# BEGIN WordPress
# WPhtc: Begin Custom htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have recently changed my wordpress site from http to https... The problem is that old url's redirect to the domain name instead of the https version of that page
eg
if I access the following page https://domain.com/test/testing/ it works 100%, now if I change the https part to http then the page redirects to https://domain.com instead of to https://domain.com/test/testing/ how do I fix it so that if you go to the old version page http://domain.com/test/testing/ (the not https version) that it redirects to https://domain.com/test/testing/ instead of just the domain name https://domain.com

You have to find a workaround for %{REQUEST_FILENAME} since this only represents the file that is accessed. But you obviously want to access the SSL vHost.
So you might hardcode the https into your .htaccess.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This might help you alot. (found the code above there)

I've been struggling also with this issue and finally I found a solution for the home redirection and the wordpress in the same htaccess file, and finally it also works for old http links, redirecting to https:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$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

I tried your solution. It worked well, but if you do that you'll need to manually change all your internal links.
This works better ;)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

I use this .htaccess for wordpress
<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>

Related

wordpress htaccess in sub folder not redirecting to https

I have a domain with a working htaccess file, which redirects all http traffic to https. This is/was working all fine.
Here is that .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.well-known/acme-challenge
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Just now, I have installed wordpress in a sub-folder called "blogg" like this:
www.example.com/blogg/
I noticed right away that the blog was not https. So I went to wordpress admin and changed the URL in general settings from "http" to "https". This seems to have made all links in wordpress, as well as the admin page, use https.
Unfortunately it didnt help when going to the blogg in the web browser, it is still http, although https works if I enter it manually in the browser.
Here is the htaccess file in the wordpress directory (example.com/blogg/.htaccess):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blogg/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blogg/index.php [L]
</IfModule>
# END WordPress
I am new to .htaccess, and would like some help in finding the right code for all my domain to be https, including the blog. Help is appreciated.
To be clear, I want all http requests to automatically go to https instead.
I have tried adding this line in the second .htaccess file: (no luck though).
RewriteCond %{HTTPS} off RewriteRule ^(.*)$
https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
BR
You can try something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blogg/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blogg/index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L] //SERVER_NAME = your actual domain name for WP
</IfModule>
# END WordPress
You can also look into this code since its an easier version. It will give you idea about WP htaccess if in root folder as well. :)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]
# 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

WordPress SSL redirects through Cloudflare

I have added my website to Cloudflare for use of flexible SSL. Everything on static content is ok. For example, I can access my files directly with https (Ex: https://www.samanik.com/logo.png).
but I can't access the WordPress site. I have changed both the site_url and home_url in the db. I have added codes to .htaccess, but when I type my website without https it redirects to https (as I want). But, nothing is shown there. I get an error called ERR_TOO_MANY_REDIRECTS I don't know how to fix this problem.
Here is my .htaccess content.
# 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
<IfModule mod_rewrite.c>
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]
</IfModule>
Can anyone help me?
You can redirect to HTTPS using action hook as well.
Add the below code in your **functions.php**:
function redirectToHTTPS(){
if($_SERVER['HTTPS']!="on"){
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}}
add_action('init','redirectToHTTPS' );
For detailed guide, check here.
in General Settings set your WordPress Address (URL) Site Address (URL) to https://...
update:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Problems with redirect, permalinks wordpress

I have a wordpress site, the url was www.mysite.com/name-of-the-post
But the client change to www.mysite.com/blog/name-of-the-post
Now I have a problem with the users who click on old links, they can't access the content.
How can I solve this? I've reading about and something with regular expressions on htaccess, how can I add "/blog" when the users trying to access old links?
Thanks guys
Where I will put this code? My htaccess is this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [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

htaccess redirects in wordpress iste

I'm moving a site from static html to the wordpress platform and trying to set up redirects but I can't seem to get it working. The site is at http://slysmidtown.com
What I would like to do is redirect requests for menu.html to /menu/
and redirect all other .html file requests to index.php or just http://slysmidtown.com
this is what I have so far the top line almost works so that the site will load but still requests the html file the rest is written by wordpress
RewriteEngine on
rewriterule ^menu.html(.*)$ http://slysmidtown.com/menu/$1 [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
You forgot that you need to rewrite it back internally to a file, since Apache doesn't know how to handle /menu/ urls now. To prevent infinite redirects you need to let the redirect only match on an external request:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /menu\.html\ HTTP
RewriteRule ^ /menu/ [R,L]
RewriteRule ^menu/?$ /menu.html [L]
You had the rule in the wrong place, placing it just after the rewritebase should work/:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Rewriterule ^menu.html(.*)$ http://slysmidtown.com/menu/$1 [r=301,nc]
Rewriterule ^(.*).html(.*)$ http://slysmidtown.com/ [r=301,nc]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

.htaccess HTTPS to HTTP Not working

I am trying to redirect visits to https://www.domain.co.uk to http://www.domain.co.uk. I have researched this, but have come to a dead end.
Many people including people here, on SO suggest to use:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This is my entire .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I have tried putting the https rewrite code above, below and within the <IfModule mod_rewrite.c>. But had no luck.
I know the .htaccess is being read, as I can make it return a 500 internal server error when using random letters.
Thanks for your help.
EDIT
Anubhava found out my server isn't listening for port 443 as my VirtualHost has no entry for it.
Here's that section of my httpd.conf
If it is WP then in your permalink settings change your site's URL to http://domain.com which seems to be https://domain.com at present. WP forces that URL if you're using a different one.
Your full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Categories