dpwtr666638 2016-02-20 23:38
浏览 56
已采纳

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"

  • 写回答

1条回答 默认 最新

  • doubingguan3425 2016-02-21 14:23
    关注

    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!)

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘