Apache ModRewrite keeps redirection

I'm trying to set up 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 host :

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\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>

      

.htaccess (found 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 in C:\websites\learn\yii-v1\blog\index.php

. When I login learn.loc/yii

, the web browser is redirected indefinitely. The trace ModRewrite

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

      

Note that the request IDs (first column) are different, so some kind of infinite recursion is happening and I can't figure out why. No Apache or PHP errors.

+3


source to share


1 answer


You have to bind the 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 also starts with /yii

, hence it keeps rewriting until the Apache module mod_rewrite

reaches the maximum limit set at the Apache server level, or the default, i.e. e. 10

...



This limit can be changed in the Apache configuration with this directive:

RewriteOptions MaxRedirects=20

      

+5


source







All Articles