Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

regex - Apache mod_rewrite REDIRECT_STATUS condition causing directory listing

I have the following htaccess rewrite rules. The one rule condition to prevent looping was originally written this way:

RewriteCond %{ENV:REDIRECT_STATUS} ^.

It used to work just fine, until it suddenly stopped working causing Apache to display the directory listing of the website.

I had to change it to this new form, as in the listing below, to have it work again:

RewriteCond %{ENV:REDIRECT_STATUS} 200

Do you have any idea of the reason of this behaviour?

Thank you

RewriteEngine on
RewriteBase /

## Permanent 301

## Force to www. Un-comment in production.
RewriteCond %{HTTP_HOST} !^www.myhost.com [NC]
RewriteRule ^(.*) http://www.myhost.com/$1 [L,R=301]

## Permanent redirect rules for contents

RewriteRule ^argument/programming/?$ tags/programming [NC,L,R=301]

## Internal Redirect Loop Protection
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

## Maintenance page
#RewriteRule (.*) special/maintenance.html

## Specials
RewriteRule special/(.*) special/$1 [NC,L]

## Static resources
RewriteRule ^(.*.(js|ico|gif|jpg|png|css|rss|xml|htm|html|pdf|zip|gz|txt))$ public/$1 [NC,L]

## Front Controller
RewriteRule ^(.*) public/index.php [NC,L]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have this condition to stop looping:

## Internal Redirect Loop Protection
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

This works by checking internal Apache variable %{ENV:REDIRECT_STATUS}. This variable is empty at the start of rewrite module but is set to 200 when first successful internal rewrite happens. This above condition says bail out of further rewrites after first successful rewrite and stops looping.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...