我正在使用此代码从我的网址末尾删除.php,使其更像SEF。
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
这是通用的,适用于每个UR,但我想知道如何为一个且只有一个URL创建例外,即:
http://www.domain.com/services
到http://www.domain.com/services.php
不影响所有其他人。这可能吗?
我尝试了一个简单的重定向但是进入了一个循环。
我没有提到我也有这些规则:
添加www。
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
删除索引(索引的扩展名.php已被修剪)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
答案 0 :(得分:2)
你应该:
RewriteCond
添加到已停用的services.php => services
services => services.php
,因为第一条规则不会阻止用户手动使用services
网址 完整.htaccess
# domain.tld > www.domain.tld (visible)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# services > services.php (visible)
RewriteRule ^services/?$ services.php [R=301,QSA,L]
# filename > filename.php (transparent)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
# filename.php > filename (visible, exception for services.php)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteCond %{REQUEST_FILENAME} !services\.php
RewriteRule ^ /%1 [R=301,NE,L]
# index > /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
答案 1 :(得分:1)
我认为只需对您的.php
删除规则进行一些小改动就可以完成这项任务:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+?)\.php[\s?] [NC]
RewriteRule !^services\.php$ /%1 [R=301,NE,NC,L]
答案 2 :(得分:0)
你能试试吗,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
答案 3 :(得分:0)
您可以为每个php文件添加一条规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^services /services.php [L]