首页 > 解决方案 > 使用 htaccess 获取带有和不带有斜杠的漂亮 URL?

问题描述

这是我当前的代码:

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

此代码适用于此 URL:

https://example.com/test-page

但它不适用于此:

https://example.com/test-page/

有没有办法让它对两者都有效?

标签: apache.htaccess

解决方案


您可以为此使用两个单独的规则,

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ $1.php [L,OR]
RewriteRule ^(.+)$ $1.php [L]

推荐阅读