删除的.php与mod_rewrite的 [英] Removing .php with mod_rewrite

查看:111
本文介绍了删除的.php与mod_rewrite的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重写所有我的网址隐藏的.php 扩展。一般的规则,增加了的.php 来的所有的网址,显然不会做的,所以我想这个规则是一个事实,即条件

I want to rewrite all my URLs to hide the .php extension. A general rule that adds .php to all URLs obviously won't do, so I want this rule to be conditional on the fact that


  • 没有文件也没有目录的网址
  • 匹配
  • 添加在的.php ,还有的匹配

  • There is no file nor directory that matches the URL
  • After adding the .php, there is a match

我在这些重写规则到达:

I've arrived at these rewriting rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

前两个照顾检查请求是否可以不用重写(因此对于图像和其他非PHP内容)满意的。随着第三条规则,我想检查是否添加的.php 将使请求有效(如果没有,我们将进一步别的东西尽量往下重写规则,或返回404)。

The first two take care of checking whether the request can be satisfied without rewriting (so for images and other non-PHP content). With the third rule I'd like to check if adding .php would make the request valid (if not, we'll try something else further down the rewrite rules, or return 404).

的RewriteCond%{} REQUEST_FILENAME -f的.php 部分似乎永远不会,虽然相匹配。有什么建议?

The RewriteCond %{REQUEST_FILENAME}.php -f part never seems to match though. Any suggestions?

修改

重写日志包含这些条目:

The rewrite log is containing these entries:

(4) RewriteCond: input='/contact' pattern='!-f' => matched 
(4) RewriteCond: input='/contact' pattern='!-d' => matched 
(4) RewriteCond: input='/contact.php' pattern='-f' => not-matched

我觉得奇怪的是它试图匹配/接触,而不是在/ var / WWW / mysite的/ htdocs中/触点(至少这就是我想要阅读 REQUEST_FILENAME )。显然 REQUEST_FILENAME 只是我所期望的,当有一个实际的比赛?

I find it strange that it's trying to match /contact, and not /var/www/mysite/htdocs/contact (At least that's what I'd expect when reading the docs on REQUEST_FILENAME). Apparently REQUEST_FILENAME is only what I expect when there is an actual match?

EDIT2

感谢 的托马斯的答案:使用%{DOCUMENT_ROOT} 代替手工撰写尚未unfound PHP文件的完整的文件名。

Thanks to Thomas for the answer: use %{DOCUMENT_ROOT} instead to manually compose the complete filename of the as yet unfound PHP file.

我也抛出一个额外的/?在正则表达式来删除一个可选的/在URL月底,现在所有的/接触,/联系人/和/contact.php都指/contact.php:

I've also thrown in an extra '/?' in the regexp to remove an optional / at the end of the URL, now all of /contact, /contact/ and /contact.php all refer to /contact.php:

RewriteCond %{DOCUMENT_ROOT}$1 !-f
RewriteCond %{DOCUMENT_ROOT}$1 !-d
RewriteCond %{DOCUMENT_ROOT}$1\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

更新

在很多骂我还发现,这取决于的RewriteRules是否是一个&LT内部; Directory>指令或.htaccess文件,一方面,或在您的<虚拟主机>指令,或只是在主体的另一方面的httpd.conf中。当内<目录> / htaccess的,%{REQUEST_FILENAME} 不包含路径,所以我最初的解决方案应该在那里工作。的&LT之外;目录>,你需要添加%{DOCUMENT_ROOT} 亲自

After a lot of cursing I also found out that it depends on whether the RewriteRules are inside a <Directory> directive or .htaccess file on the one hand, or in your <VirtualHost> directive or just in the main body of an httpd.conf on the other hand. When inside a <Directory>/.htaccess, %{REQUEST_FILENAME} does contain the path so my initial solution should work there. Outside of <Directory>, you need to add the %{DOCUMENT_ROOT} yourself.

推荐答案

我不太确定如何使用REQUEST_FILENAME。 REQUEST_FILENAME应该指向匹配文件的完整路径,但你实际上是检查它的存在。

I'm not so sure about using REQUEST_FILENAME. REQUEST_FILENAME should point to the full path of the matched file, but you are actually checking it's existence.

我会尝试这样的:

RewriteCond  %{DOCUMENT_ROOT}/$1         !-f
RewriteCond  %{DOCUMENT_ROOT}/$1\.php    -f
RewriteRule  ^(.*)$                      $1.php [L]

这篇关于删除的.php与mod_rewrite的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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