N
N
Nikita Borzenkov2015-11-12 18:06:16
Apache HTTP Server
Nikita Borzenkov, 2015-11-12 18:06:16

How to use .htaccess to remove the file extension and add a slash at the end of the url?

Actually the question is in the title. Now my .htaccess only removes the extension. How else to make a slash at the end?

<IfModule mod_rewrite.c>
  # enable the rewrite engine
  RewriteEngine On
  # Set your root directory
  RewriteBase /

  # remove the .html extension
  RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
  RewriteRule (.*)\.html$ $1 [R=301]

  # remove index and reference the directory
  RewriteRule (.*)/index$ $1/ [R=301]

  # remove trailing slash if not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*)/ $1 [R=301]

  # forward request to html file, **but don't redirect (bot friendly)**
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule (.*) $1\.html [L]
  
</IfModule>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question