【问题标题】:500 Internal error with .htaccess only on localhost - redirect loop.htaccess 仅在本地主机上出现 500 内部错误 - 重定向循环
【发布时间】:2014-01-25 04:51:29
【问题描述】:

我有以下 .htacess 文件来删除 .php 扩展名并创建 SEO 友好 URL:

RewriteEngine On 

ErrorDocument 404 /404.php

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

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

当托管在我们的域上时,这绝对可以正常工作,但是当我在本地主机服务器上进行本地测试时,它会出现错误 500,内部服务器错误。

在我的 httpd.conf 文件中,rewrite 模块没有注释:

LoadModule rewrite_module modules/mod_rewrite.so

Apache 错误日志指出:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://localhost:8888/Website/

有什么想法吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite http-redirect


    【解决方案1】:

    是的,这肯定是由于循环。

    您的第一条规则是删除 .php 扩展名。然后 404 处理程序和第二条规则将所有内容发送到 .php 文件,因此循环将继续。

    为了避免你可以使用这些规则:

    RewriteEngine On 
    RewriteBase /Website/
    
    ErrorDocument 404 /Website/404.php
    
    RewriteCond %{THE_REQUEST} \s/+/Website/(.+?)\.php[\s?] [NC]
    RewriteRule ^ %1 [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)$ index.php?page=$1 [L,QSA]
    

    注意使用%{THE_REQUEST} 而不是RewriteRuleTHE_REQUEST 变量表示 Apache 从您的浏览器收到的原始请求,它不会因应用其他规则而改变。

    【讨论】:

    • 感谢您的帮助,但似乎有同样的问题。
    • 还有其他规则或任何其他 .htaccess 吗?
    • 不,您在我在 OP 中发布的 htaccess 文件中看到的是我的 .htaccess 文件中的内容。它适用于我的域,但不适用于 localhost,这似乎很奇怪。
    • OK 你能在顶部添加Options +FollowSymLinks -MultiViews 行吗。
    • 感谢您的帮助和很好的解释:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2023-03-19
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    相关资源
    最近更新 更多