%{REQUEST_URI}似乎与index.php无关

时间:2014-10-09 04:57:36

标签: regex apache .htaccess mod-rewrite

我的网站使用以下格式的链接:

http://www.website.com/section1/index.php
http://www.website.com/section2/index.php
http://www.website.com/section3/index.php
http://www.website.com/section1/section4/index.php

我试图通过使用以下.htaccess指令来删除最后一部分“index.php”:

RewriteEngine on
RewriteCond %{REQUEST_URI}  ^/index\.php$
RewriteRule (.*)  http://www.google.com [R=301,NC,L] 

当然,www.google.com仅用于测试目的,但下面的示例不起作用。这有什么问题?问题的第二部分是如果我想重写为http://www.website.com/section1,我该如何替换www.google.com?

谢谢!

1 个答案:

答案 0 :(得分:1)

由于你的正则表达式不正确,它无效。 RewriteCond %{REQUEST_URI} ^/index\.php$期待%{REQUEST_URI}/index.php,但您有/section1/index.php

正确的版本将是:

RewriteEngine on

RewriteCond %{REQUEST_URI} /index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L] 

甚至:

RewriteEngine on

RewriteRule /index\.php$ http://www.google.com [R=301,NC,L]