Apache mod_rewrite - 比具有漂亮 URL 的目录更喜欢文件 [英] Apache mod_rewrite - prefer files over directories with pretty URLs

查看:15
本文介绍了Apache mod_rewrite - 比具有漂亮 URL 的目录更喜欢文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要漂亮的网址,所以 http://www.domain.com/foo 会返回 http://www.domain.com/foo.php

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

问题是存在同名的目录.我有另一个页面 http://www.domain.com/foo/bar/baz 现在我的服务器只在我请求 http://www.domain.com 时返回 foo 的目录列表/foo

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

伪代码:
如果请求加上.php"是一个文件
重写文件而不是目录

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

实际代码:

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

推荐答案

需要使用全路径:

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

REQUEST_FILENAME 只是 DocumenRoot

这篇关于Apache mod_rewrite - 比具有漂亮 URL 的目录更喜欢文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆