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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀