dqhgjay5753 2017-08-07 14:50
浏览 74
已采纳

Apache Mod_Rewrite保持重定向

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. 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:

<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
oop.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)

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. When I enter learn.loc/yii, the web browser is redirected indefinitely. The ModRewrite trace shows:

[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. There are no Apache or PHP errors.

  • 写回答

1条回答 默认 最新

  • dsirr48088 2017-08-07 14:52
    关注

    You must anchor your regex pattern in RewriteRule to avoid this problem:

    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 i.e. 10.

    This limit can be changed in Apache config using this directive:

    RewriteOptions MaxRedirects=20
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?