如何通过RewriteRule仅重定向主Root / index.php而不是Root / Folder / index.php?

时间:2011-03-18 18:53:18

标签: apache mod-rewrite url-rewriting apache2 indexing

亲爱的朋友们, 目前重写应该重定向

/index.php/en/home

为此,通过以下规则可以正常工作:

RewriteRule ^index.php /en/home [R=301]

然而,当/someotherfolder/index.php被调用时,即使它被重定向到/en/home,它也不应该!如何将其硬编码为仅重写,条件是它的root-index.php文件,而不仅仅是位于其他更深文件夹中的任何index.php文件?

非常感谢您的建议!非常感谢。

2 个答案:

答案 0 :(得分:1)

最好调试RewriteRule,就是打开重写过程的日志记录。

<VirtualHost x.x.x.x>

  ...

  RewriteEngine On
  RewriteLog "/var/log/httpd/rewrite_log"
  RewriteLogLevel 3

  RewriteRule .....................

  ...
</VirtualHost>

有一个问题,是VirtualHost部分或目录部分内的RewriteRule吗?

-Martin

答案 1 :(得分:1)

这是一个工作样本,希望这有帮助。

RewriteLog "/var/log/httpd/rewrite_log"
RewriteLogLevel 3

Alias /dummy.org /tmp/dummy.org

<Directory /tmp/dummy.org>
  Options FollowSymLinks
  RewriteEngine On
  RewriteRule ^index.htm /en/somepage [R=301]
</Directory>

如果我使用“http://127.0.0.1/dummy.org/index.htm”,它会被重写为“http://127.0.0.1/en/somepage”

(3) [perdir /tmp/dummy.org/] strip per-dir prefix: /tmp/dummy.org/index.htm -> index.htm
(3) [perdir /tmp/dummy.org/] applying pattern '^index.htm' to uri 'index.htm'
(2) [perdir /tmp/dummy.org/] rewrite 'index.htm' -> '/en/somepage'
(2) [perdir /tmp/dummy.org/] explicitly forcing redirect with http://127.0.0.1/en/somepage
(1) [perdir /tmp/dummy.org/] escaping http://127.0.0.1/en/somepage for redirect
(1) [perdir /tmp/dummy.org/] redirect to http://127.0.0.1/en/somepage [REDIRECT/301]

如果我使用“http://127.0.0.1/dummy.org/someotherfolder/index.htm”,它就不会被重写

(3) [perdir /tmp/dummy.org/] add path info postfix: /tmp/dummy.org/someotherfolder -> /tmp/dummy.org/someotherfolder/index.htm
(3) [perdir /tmp/dummy.org/] strip per-dir prefix: /tmp/dummy.org/someotherfolder/index.htm -> someotherfolder/index.htm
(3) [perdir /tmp/dummy.org/] applying pattern '^index.htm' to uri 'someotherfolder/index.htm'
(1) [perdir /tmp/dummy.org/] pass through /tmp/dummy.org/someotherfolder

-Martin