Apache mod_rewrite - prefer files over directories with pretty URLs

拟墨画扇 提交于 2019-12-30 07:43:26

问题


I want to have pretty urls so http://www.domain.com/foo will return http://www.domain.com/foo.php

The issue is that there is a directory that has the same name. I have another page at http://www.domain.com/foo/bar/baz and right now my server just returns the directory listing of foo when I request http://www.domain.com/foo

Pseudocode:
If the request plus ".php" is a file
rewrite out the file instead of the directory

Actual Code:

RewriteEngine On  
RewriteBase /  
RewriteCond %{SCRIPT_FILENAME}\.php -f  
RewriteRule (.*) $1.php [NC,L]

回答1:


MultiViews could cause this behavior. Try to disable it.




回答2:


You need to use the whole path:

RewriteCond "%{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php" -f  

REQUEST_FILENAME is just the full path inside DocumenRoot



来源:https://stackoverflow.com/questions/2659830/apache-mod-rewrite-prefer-files-over-directories-with-pretty-urls

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