好的,所以我有这个mod_rewrite规则在内部附加.html扩展名,如果在发送请求时没有提供它:
Options +FollowSymLinks RewriteEngine on # ## Internally rewrite extensionless file requests to .html files ## # # If the requested URI does not contain a period in the final path-part RewriteCond %{REQUEST_URI} !(\.[^./]+)$ # and if it does not exist as a directory RewriteCond %{REQUEST_FILENAME} !-d # and if it does not exist as a file RewriteCond %{REQUEST_FILENAME} !-f # then add .html to get the actual filename RewriteRule (.*) /$1.html [L] # # ## Externally redirect clients directly requesting .html page URIs to extensionless URIs # # If client request header contains html file extension RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP # externally redirect to extensionless URI RewriteRule ^(.+)\.html$ http://www.example.com/$1 [R=301,L]
我想在这个文件的第一部分中将.html改为.php的任何地方进行更改,以便当有人在不添加扩展名的情况下请求.php文件时,它仍会转到该.php页面。事实证明,这不起作用。为什么不呢?
答案 0 :(得分:1)
请尝试以下处理php的规则:
## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .html to get the actual filename
RewriteRule (.*) /$1.php [L]
## Externally redirect clients directly requesting .php page URIs to extensionless URIs
#
# If client request header contains html file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.php
# externally redirect to extensionless URI
RewriteRule ^(.+)\.php$ /$1 [R=301,L]