I want to rewrite some URLs with .php
to trailing slash URLs. However, I don’t want to rewrite all URLs.
My code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET /[^?s]+.php
RewriteRule (.*).php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_URI} !.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$
RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
I want force slash on:
- /about-us.php => /about-us/
- /services.php => /services/
I don’t need slash on:
- /cart.php => /cart.php
- /auth.php => /auth.php
2
Answers
You can use this to remove the trailing slash from your
/cart
and/auth
URIs .Put the following two rules right below
RewriteBase
line in your htaccess :You may use these rules in your site root .htaccess:
Make sure to use only these rules in root .htaccess and test from a new browser or command line
curl
.