【发布时间】:2019-01-24 15:09:32
【问题描述】:
我想将一些带有 .php 的 URL 重写为尾部斜杠 URL。但是,我不想重写所有 URL。
我的代码:
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]
我想强制使用斜线:
- /about-us.php => /about-us/
- /services.php => /services/
我不需要斜线:
- /cart.php => /cart.php
- /auth.php => /auth.php
【问题讨论】:
-
您希望
/about-us.php成为about-us.php/吗? -
@starkeen 不想要 /about-us/
-
所以在浏览器中
/about-us.php应该显示为/about-us/但/cart.php应该显示为/cart or should it just remain/cart.php` ? -
@anubhava 代码已满,/about-us.php 应显示为 /about-us/ 和 /cart.php 应显示为 /cart.php
标签: php apache .htaccess mod-rewrite url-rewriting