简体   繁体   中英

How url works with or without trailing slash using htaccess

I have htaccess in my PHP website. I want to work url with or without trailing slash.

Below code works only with the trailing slash.

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^(.*)/$ $1.php [NC,L]

eg www.mydomain.com/index/ --Works
www.mydomain.com/index --Not Work

If anybody know how can I get this without trailing slash?

Thanks

You have this rule:

RewriteRule ^(.*)/$ $1.php [NC,L]

If you want to match it regardless of / in the end use:

ErrorDocument 404 /not-found.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f will stop reapplication of same rule since $1.php will be a real/valid file.

Put a ? after the / to make it optional.

解决方法是

RewriteRule ^(.*)/?$ $1.php [NC,L]

这就是我对类似东西所做的

RewriteRule ^(.*)/?$ /$1.php [NC,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM