Wordpress Permalinks Overriding .htaccess https and force slash - wordpress

I am wanting to use the following .htaccess however it works to a point but for me to access the sub pages I have to reset my permalinks and there for this section gets reset.
It seems in Wordpress currently there is only plugins to do one or another - not both of my goals.
Goals:
To force HTTP:// to HTTPS://
Include a trailing / at the end of the url
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /subFolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subFolder/index.php [L]
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
</IfModule>

WordPress makes this block in your .htaccess file. Note the first and last line (I added "subfolder/" to fit your example):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
WordPress in theory shouldn't be overridding anything outside of the # Begin / #End lines. So you can place your extra code above or below it.
That being said, WordPress adds a trailing slash by default when using PostName/pretty permalinks so I don't think you'd need anything extra. And generally if you set the Site URL to have https, all generated links will be over https as well, with the notable exception of existing media/links in the content editor. For those, add this below the #End WordPress above (switch example.com to your domain):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Related

redirection gone wrong htaccess

The url gets redirected to https://2day.news/ instead of https://www.2day.news/.
This happens on every browser/mobile, cleared caches. I don't think this has anything to do with a server back-up as it goes blank when I change the redirect to a page that doesn't exist.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^2day.news [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.2day.news/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Add 301, www and https after RewriteBase
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.2day\.news$ [NC]
RewriteRule ^(.*)$ https://www.2day.news/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
On some servers, redirecting from non-www to www in the .htaccess will cause Too many redirections error. So I recommend changing the siteurl in the database.
You can change the URL of the site in the wp_options table, in the option_value column of the following options: siteurl, home.
Or alternatively in the WP admin, under Settings > General (Wordpress URL and Site URL).
You need to add the www. prefix for both options.
Please note:
Both records should be the same (or some functions may not work because of cross-domain issues)
You need to login again to WP admin after the changes

Redirecting WordPress page to HTTPS - not redirecting

Pages on our WordPress site are not automatically redirecting to the HTTPS version even though we have set the site url and home url to use HTTPS.
This is the code we have at the top of our wp-config.php file:
define('WP_HOME','https://webwisemedia.co.uk');
define('WP_SITEURL','https://webwisemedia.co.uk');
define('FORCE_SSL_ADMIN', true);
Our .htaccess file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
If you visit a page on our site, like http://webwisemedia.co.uk/web-wise-in-the-press/, it doesn't redirect to the HTTPS version.
Any ideas why?
You mixed up the Wordpress part of .htaccess and that is why it isn't working. That part is changed every time you update, for example, permalinks. Your .htaccess should look like this:
# Rewrite HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,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
This leaves the Wordpress part intact and it forces to use https. From now on if you change anything in .htaccess do it below the Wordpress part.

Issue with .htaccess redirecting all pages to HTTPS except one

I have the code below in my .htaccess to redirect all pages to https except one (/wc-api/v2), which must NOT use SSL.
It does redirect all pages to https successfully, but if I go to /wc-api/v2, it redirects me to /index.php.
# Custom Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteRule (.*) https://example.com/$1 [R,L]
</IfModule>
# End Custom Redirects
# 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
FYI The second block of redirects is required for Wordpress. I also cannot combine them into one single block or Wordpress will overwrite my changes.
When rewriting is configured in .htaccess context, after your last rule is applied, the whole process starts over again, using the internally rewritten URL as the new “input”, until no more rules match.
That means, when you request /wc-api/v2, it is not rewritten to HTTPS, because it fails your first RewriteCond, and so the rule in the next block rewrites it to /index.php internally.
Now the “next round” starts, with /index.php as input. RewriteCond %{REQUEST_URI} !^/wc-api/v2 now does result in “true”, because the REQUEST_URI is not /wc-api/v2 any more, it is now /index.php. Therefor, the RewriteRule is applied – and you are redirected to https://example.com/index.php
To avoid this, you must add another RewriteCond, which will prevent the rule from being applied for the REQUEST_URI /index.php as well:
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://example.com/$1 [R,L]
I got an error on my server with the suggestion above so modified it slightly and found that this worked for me:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://www.example.com/$1 [R,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
Hope it works for you.

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]

This webpage has a redirect loop - Wordpress + custom rewrites

I'm trying to run a site that routes all URI requests through an index.php file for processing, but I'm also trying to run Wordpress at the webroot. Everything works fine, except for requests to https://mysite.com/wp-admin/, which gives me a "This webpage has a redirect loop" error. Going to https://mysite.com/wp-admin/edit.php works fine.
My .htaccess file looks like this:
RewriteEngine On
# eliminate www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# force SSL
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}/%1 [R,L]
RewriteBase /
# rewrite directory patterns
RewriteRule ^([a-zA-Z0-9-\/]+)/([a-zA-Z0-9-%/\/]+)/([0-9]+)$ /index.php?page=$1&action=$2&id=$3 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9-\/]+)/([a-zA-Z0-9-%/\/]+)$ /index.php?page=$1&action=$2 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9-\/]+)$ /index.php?page=$1 [NC,QSA,L]
# Wordpress
<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any ideas what's causing the redirect loop? I've tried putting
RewriteCond %{REQUEST_URI} !wp-admin
in front of the force SSL and eliminate www rules, but it doesn't seem to make a difference
RewriteRule ^wp-admin - [L]
Add this rule at the top, above all other rules.

Resources