505在.htaccess文件中传递查询字符串变量时出错

I have to pass a query string variable "_page" through .htaccess file, that gives 505 (internal server) error.

This is my .htaccess file:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Make sure that there is ALWAYS a querystring variable named "__page"
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.(php|xml))$ $1?__page=$1 [L,QSA]

#Append ".php" to the requested file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php?__page=$1.php [L,QSA]
</IfModule>

When i comment the line

#RewriteRule ^(.*\.(php|xml))$ $1?__page=$1 [L,QSA]

in .htaccess file , the 505 goes , but it shows a warning " Undefined index: __page"

On *.php and *.xml requests you are getting a 500 error because your rule is causing an infinite loop error on your server.

#RewriteRule ^(.*\.(php|xml))$ $1?__page=$1 [L,QSA]

The rule above rewrites

  • /foo.php => /foo.php?__page=foo.php

And on the second rewrite iteration

  • /foo.php?__page=foo.php => /foo.php?__page=foo.php

As you can see ,on the second rewrite iteration the target url is rewriting to itself. this goes on untill 10th iteration and the server returns a 500 error.

To avoid the infinite loop error, you must exclude the destination path you are rewriting to.

And in your case the destination path is :

/foo.php?__page=foo.php

To exclude this from rewrite ,You will use

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*\.(php|xml))$ $1?__page=$1 [L,QSA]

which means "no query strings", The condition says if there is no query strings then the rule will be applied. This will ignore the second iteration.

(Hope this helps!)