【问题标题】:Rewriting every URL to index.php using .htaccess使用 .htaccess 将每个 URL 重写为 index.php
【发布时间】:2011-12-11 13:09:59
【问题描述】:

我正在尝试捕获每个 URL 并将其重写为 index.php。我现在的 .htaccess 中有以下代码:

RewriteEngine on
RewriteRule .* index.php

它确实重写了每个 URL,但我总是得到一个 404,说找不到 index.php 文件。它正在寻找文件的路径是正确的。我在这里做错了什么?


更新 1

当我直接浏览到 index.php 时,它确实正确显示了该文件。很奇怪。


更新 2

我在 httpd.conf 中使用它打开了 mod_rewrite 的日志记录:

RewriteLog /var/log/apache2/rewrite.log
RewriteLevel 3

这是它记录的内容:

strip per-dir prefix: /Users/rits/Sites/test/ -> 
applying pattern '.*' to uri ''
rewrite '' -> 'index.php'
add per-dir prefix: index.php -> /Users/rits/Sites/test/index.php
internal redirect with /Users/rits/Sites/test/index.php [INTERNAL REDIRECT]

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    我很确定这是因为它试图将index.php 无限地重写为index.php

    请尝试以下方法:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index\.php
    RewriteRule .* index.php
    

    这应该可以防止重写自身,并希望能解决您的问题。

    【讨论】:

    • 嗯,好的。那么,我不确定是什么导致了这个问题。我习惯于重定向导致 HTTP 500 错误的错误,而不是 404。Erm... 如果你尝试这个会发生什么:RewriteRule ^(.*)$ index.php?file=$1?
    【解决方案2】:

    这是来自我的一个网站的 .htaccess,我设置它以将所有内容重定向到索引页面,如果您想尝试它,它对我有用:

    RewriteEngine on
    RewriteOptions MaxRedirects=10
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !index\.php
    RewriteRule ^.*$ http://www.mywebpage.com/ [R=301,L]
    

    【讨论】:

      【解决方案3】:

      试试这个:

      RewriteEngine on
      RewriteRule ^(.*)$ http://www.yourwebpage.com/$1 [L,R=301]
      

      【讨论】:

        【解决方案4】:

        这是我拥有和工作的.htaccess 文件的内容:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-04
          • 2013-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-25
          相关资源
          最近更新 更多