0

When I enter a URL in my browsers address bar in the form https://myurl.com/path, I'd like the rewrite to be https://myurl.com/path/

In .htaccess, my rewrite rules as as follows:

#RewriteEngine On 
#RewriteCond %{HTTP_HOST} !^myurl.com$
#RewriteRule ^(.*)$ http://myurl.com/$1 [R=301,L]
#RewriteCond %{SERVER_PORT} 80 
#RewriteRule ^(.*)$ https://myurl.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://myurl.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [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>
Mattypants
  • 514
  • 5
  • 15

1 Answers1

1

I've been doing quite a bit of research into this the last couple days. It looks like you're close. Try the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$        /$1$2/ [R=301,L]

The RewriteCond will check to make sure there's no files with that name, and if not, perform the RewriteRule. More future-proof than having a manual list of extensions!

This is a copy/paste from the following question and answer: I'm not that great with .htaccess but like I said, I've been doing a lot of research on it the last few days. .htaccess Rewrite to Force Trailing Slash at the end

The [R=301] will also add a 301 redirect to the url for search engines, so they know to look at the slashed version of the page in the future - this helps reduce duplicate content and consolidate analytic information.

Hope that helps.

Community
  • 1
  • 1
MrMr
  • 26
  • 2
  • Thanks @MrMr that worked :) I didn't personally add the rules to this .htaccess file, someone else did, so I can't take credit for being close to the solution.. I'm not sure why the .html rule was added, all pages are in the form https://myurl/path/page/. Are you suggesting that I replace the following lines: `RewriteCond %{REQUEST_FILENAME}.html -f` and `RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]` only ? – Mattypants Aug 27 '16 at 09:03
  • The one problem I'm now experiencing is my XML sitemap is now getting a trailing slash, i.e., /sitemap.xml/. Interestingly--and thankfully--pdf files aren't getting a trailing slash. – Mattypants Sep 11 '16 at 04:57