简体   繁体   English

Apache ModRewrite保持重定向

[英]Apache ModRewrite keeps redirecting

I'm trying to setup a site on my local Windows machine (PHP, Apache 2.4) and my redirect rules are creating an inifinite loop. 我正在尝试在本地Windows计算机(PHP,Apache 2.4)上设置站点,并且我的重定向规则正在创建无限循环。 I can't figure out why. 我不知道为什么。

My hosts file: 我的主机文件:

127.0.0.1   learn.loc
127.0.0.1   www.learn.loc

My Apache httpd-vhosts.conf : 我的Apache httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "C:\websites\learn"
    ServerName learn.loc
    ServerAlias www.learn.loc
    ErrorLog "logs/learn.loc-error.log"
    CustomLog "logs/learn.loc-access.log" common

    LogLevel alert rewrite:trace2

    #PHP SETTINGS
    php_value auto_prepend_file "C:\websites\learn\noop.php"
    php_value open_basedir "C:\websites\learn"
    php_value error_log "C:\websites\learn\php_error.log"

    <Directory "C:\websites\learn">
      Options Indexes FollowSymLinks
      AllowOverride All
      Require all granted
    </Directory>  
</VirtualHost>

The .htaccess (located in C:\\websites\\learn ) .htaccess (位于C:\\websites\\learn

RewriteEngine on
RewriteRule ^yii   /yii-v1/blog
RewriteRule ^yii/(.*)   /yii-v1/blog/$1

The webpage I'm trying to access is at C:\\websites\\learn\\yii-v1\\blog\\index.php . 我要访问的网页位于C:\\websites\\learn\\yii-v1\\blog\\index.php When I enter learn.loc/yii , the web browser is redirected indefinitely. 当我输入learn.loc/yii ,Web浏览器将无限期重定向。 The ModRewrite trace shows: ModRewrite跟踪显示:

[rid#1330178/initial] rewrite 'yii/blog/' -> '/yii/blog'
[rid#1330178/initial] trying to replace context docroot C:/websites/learn with context prefix 
[rid#1330178/initial] internal redirect with /yii/blog [INTERNAL REDIRECT]
[rid#1331880/initial/redir#1] rewrite 'yii/blog' -> '/yii/blog'
[rid#1331880/initial/redir#1] trying to replace context docroot C:/websites/learn with context prefix 
[rid#1331880/initial/redir#1] internal redirect with /yii/blog [INTERNAL REDIRECT]
[rid#1334a40/initial/redir#2] rewrite 'yii/blog' -> '/yii/blog'
[rid#1334a40/initial/redir#2] trying to replace context docroot C:/websites/learn with context prefix 
[rid#1334a40/initial/redir#2] internal redirect with /yii/blog [INTERNAL REDIRECT]
[rid#1335b88/initial/redir#3] rewrite 'yii/blog' -> '/yii/blog'
[rid#1335b88/initial/redir#3] trying to replace context docroot C:/websites/learn with context prefix 
[rid#1335b88/initial/redir#3] internal redirect with /yii/blog [INTERNAL REDIRECT]
[rid#1336e30/initial/redir#4] rewrite 'yii/blog' -> '/yii/blog'
...

Notice that the request ids (first column) are different so there is some sort of infinite recursion happening, and I can't figure out why. 请注意,请求ID(第一列)不同,因此发生了某种无限递归,我不知道为什么。 There are no Apache or PHP errors. 没有Apache或PHP错误。

You must anchor your regex pattern in RewriteRule to avoid this problem: 您必须在RewriteRule锚定正则表达式模式,以避免出现此问题:

RewriteEngine on

RewriteRule ^yii/?$   /yii-v1/blog [L,NC]

RewriteRule ^yii/(.+)$ /yii-v1/blog/$1 [L,NC]

Your regex pattern ^yii matches any URI that starts with /yii and since your rewritten URI is also starting with /yii hence it keeps rewriting until Apache mod_rewrite module reaches max limit set at Apache server level or by default ie 10 . 您的正则表达式模式^yii与以/yii开头的任何URI匹配,并且由于您重写的URI也以/yii开头,因此它将一直进行重写,直到Apache mod_rewrite模块达到Apache服务器级别设置的最大限制或默认为10为止。

This limit can be changed in Apache config using this directive: 可以使用以下指令在Apache配置中更改此限制:

RewriteOptions MaxRedirects=20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM