.htaccess Messing up with .php extension?

非 Y 不嫁゛ 提交于 2020-05-09 06:08:09

问题


Ok, so I have this mod_rewrite rule that internally attaches a .html extension if it is not provided when sending the request:

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]

I thought about changing anywhere it says .html to .php in the first part of this file so that when someone requests a .php file without adding it's extension, it would still go to that .php page. Turns out, this doesn't work. Why not?

My evidence here: http://appstorecrazy.com/OHNOEZ/NOTEST


回答1:


Try following rules for handling 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]


来源:https://stackoverflow.com/questions/5724014/htaccess-messing-up-with-php-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!