0

I have the following .htaccess setup:

RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

What I want to achieve:

  1. Redirect from http to https
  2. Redirect from the non-www to www
  3. Add trailing slash

The first two points are ok, the redirects are working as expected, but the third one doesn't.

How can I adapt the existing rules in order to add the trailing slash.

Cosmin
  • 864
  • 3
  • 16
  • 34

1 Answers1

0

You may use these rules in your .htaccess:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# add a trailing slash to non-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

Make sure to use a new browser for testing the updated rules.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Implemented the new rules as suggested and tried the links with a new browser, but it doesn't seem to work. Still no trailing slash added. – Cosmin Mar 09 '18 at 18:46
  • Unfortunately I can't paste the URL here. – Cosmin Mar 09 '18 at 18:49
  • 1
    Ok, then the URL is like this: https://www.example.com/articles/the-title-of-the-article-758 – Cosmin Mar 09 '18 at 18:52
  • This is new since you never told about any 404? What URL are you entering in browser that causes 404? Also update your question with your full .htaccess? – anubhava Mar 09 '18 at 19:05
  • I get a 404 when I add the URL without the trailing slash. – Cosmin Mar 09 '18 at 19:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166558/discussion-between-anubhava-and-cosmin). – anubhava Mar 09 '18 at 19:17