I need help with my .htaccess file. I want to have a 301 redirect from HTTP to HTTPS and from www to non-www with a trailing slash at the end. I can't remove trailing slash from posts because WordPress adds it at the end of the URL and when I remove it all my posts return a 404 error and some plugins don't work.
I ended with results like:
I'm new on this forum so I can't add more than 8 links so I made a screenshot with more details. enter image description here
Current .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Is there any way to get rid of the bolded part to have only one 301 redirects?
Have your rules like this:
RewriteEngine On
# add a trailing slash with www removal and https
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule [^/]$ https://%1%{REQUEST_URI}/ [L,NE,R=301]
# handle www->non www, http->https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]