apache - 使用 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/
有没有办法让它对两者都有效?
解决方案
您可以为此使用两个单独的规则,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ $1.php [L,OR]
RewriteRule ^(.+)$ $1.php [L]
推荐阅读
- python - Can i create a real time chart with Pygal?
- frontend - Noob question regarding databases and frontends
- javascript - Javascript / Webix将对象引用为数组
- .net - Is it possible to configure the file name of a RollingFileAppender to include date followed by process ID?
- c++ - 没有规则使用 CMAKE 添加库来制作目标。库单独编译就好了
- mysql - 不使用 with 子句重写 sql
- docker - 我在 docker 容器中运行 cassandra。通过复制 cassandra 表生成的文件存储在哪里?
- asp.net - 为什么 web.config 文件会为 CSP 中的不安全内联引发无法识别的错误?
- python - 无法在 mac os x mojave 上安装 mysqlclient
- http - Google App Engine 上的封闭正文读取无效