【问题标题】:.htaccess causes infinite loop on live server but works on localhost.htaccess 在实时服务器上导致无限循环,但在本地主机上工作
【发布时间】:2015-09-04 03:41:25
【问题描述】:

我是 .htaccess 和正则表达式的新手。我有一个可以在本地运行的脚本,但是当我将它上传到实时服务器时,它会导致无限循环。我怎样才能解决这个问题?在站点上,当您加载 http://example.com 时,如果您未经身份验证,它会将您带到 http://example.com/auth/ 以获取登录表单,否则它会显示 http:///example.com/index.php 的内容。它在本地运行良好,但是当我在实时服务器上上传时,它找不到auth.php,因此它重定向到索引页面,并且由于用户未经过身份验证,它发送到auth.php,因此无限循环。请帮忙。谢谢。

这是我的文件

Options -Indexes
Options -MultiViews    
Options +FollowSymLinks

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /     

   # remove php extensions (only for GET method)
   RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule ^ %1/? [L,R=301]

   # don't touch other existing files/folders
   RewriteCond %{REQUEST_FILENAME} -f [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^ - [L]

   # force trailing slash
   RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
   RewriteRule ^(.+)$ $1/ [L,R=301] 

   # rewrite extensionless php files
   RewriteCond %{DOCUMENT_ROOT}/$1.php -f
   RewriteRule ^(.+)/$ $1.php [L]

   #rewrite path to file eg http://example.com/auth/recover to http://example.com/auth.php?a=recover
   RewriteCond %{DOCUMENT_ROOT}/$1.php -f
   RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?a=$2 [QSA,L]

   RewriteCond %{DOCUMENT_ROOT}/$1.php -f
   RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]

   # finally, if not found
   RewriteRule ^ index.php [L]
</IfModule>

【问题讨论】:

  • 没有评论就投反对票???真的???非常感谢

标签: php regex .htaccess


【解决方案1】:

在谷歌搜索、阅读、尝试和测试之后,我找到了解决 Godaddy 有趣的 .htaccess 问题的方法。我在这里解决了。

https://serverfault.com/questions/720085/htaccess-causes-infinite-loop-on-live-server-but-works-on-localhost/

【讨论】:

    【解决方案2】:

    您可以重新排序规则:

    Options +FollowSymLinks -MultiViews -Indexes
    
    <IfModule mod_rewrite.c>
       DirectoryIndex index.php
       RewriteEngine On
       RewriteBase /     
    
       # remove php extensions (only for GET method)
       RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC]
       RewriteCond %{REQUEST_FILENAME} -f
       RewriteRule ^ %1/? [L,R=301]
    
       # don't touch other existing files/folders
       RewriteCond %{REQUEST_FILENAME} -f [OR]
       RewriteCond %{REQUEST_FILENAME} -d [OR]
       RewriteCond %{ENV:REDIRECT_STATUS} \d
       RewriteRule ^ - [L]
    
       # force trailing slash
       RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
       RewriteRule ^(.+?[^/])$ $1/ [L,R=301] 
    
       RewriteCond %{DOCUMENT_ROOT}/$1.php -f
       RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]
    
       #rewrite path to file eg http://example.com/auth/recover to http://example.com/auth.php?a=recover
       RewriteCond %{DOCUMENT_ROOT}/$1.php -f
       RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?a=$2 [QSA,L]
    
       # rewrite extensionless php files
       RewriteCond %{DOCUMENT_ROOT}/$1.php -f
       RewriteRule ^(.+?)/$ $1.php [L]
    
       # finally, if not found
       RewriteRule . index.php [L]
    </IfModule>
    

    【讨论】:

    • 仍在循环播放。该网站托管在 godaddy
    • GoDaddy 有时没有启用 htaccess。 .htaccess 的位置可能是另一个问题。为什么找不到auth.php
    • .htaccess 和 auth.php 位于同一个文件夹中,该文件夹是我提供网页服务的根文件夹,即我的 htdocs。该站点是 godaddy 托管帐户的子域。
    • 但这并不能解释为什么找不到auth.php。清除浏览器缓存后尝试我更新的规则。
    • 还是不行,我什至尝试将脚本重命名为 login.php 但仍然失败
    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 2017-06-12
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多