Search code examples
.htaccessmod-rewrite

How to clean url to accept ending slash or not with .htaccess


How do I clean URL http://localhost/mysitedirectory/contacts.php

to look like both:

http://localhost/mysitedirectory/contacts

http://localhost/mysitedirectory/contacts/

I want this because a user may add the ending trailing slash or leave it so I want .htaccess to configure such that it works both ways.

I noticed when I add the / at the end of the contacts it says "OBJECT NOT FOUND"...Error 404

How do I make it work even if I add the slash or not?

this is what I tried:

RewriteEngine On
Options -Indexes


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contacts$ contacts.php [L,QSA,NC]
RewriteRule ^home$ index.php [L,QSA,NC]

Solution

  • CBroe answered in a comment section:

    ^contacts/?$ - slash at the end, made optional by the following quantifier question mark.