doupu7651 2012-01-27 02:13
浏览 28
已采纳

找出htaccess规则以及它们无法正常工作的原因有点帮助

I have these rules and I can't figure out why one of them won't work:

RewriteEngine On
RewriteBase /

Options +FollowSymlinks
Options -MultiViews
Options -Indexes

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#RewriteRule ^error/([^/.]+)/([^/.]+)$ /pages/error.php?$1=$2 [NC,R]
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT]
RewriteRule ^error/.* error.php/?$0 [PT]

Rule 1 works, echo $_GET['user'] then I get results

Rule 2 works as well. If I do a print_r($_SERVER['REQUEST_URI']) then I get what ever is after .com/.

so That makes me think that its catching correctly, and if I do a as well.

So those rules seem to be working... but now if I enter

domain.com/error/access/notallowed then I get a Not Found error, so that means that my Rule 3 that wants to catch anything that starts with error isn't working.

Any ideas on what may be causing this behavior?

EDIT: updated rules based on answer

RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteCond %{REQUEST_URI} ^/error/?
RewriteRule ^([^/.]+)/?.* index.php?$1 [PT]
RewriteRule ^(error/?.*)$ /_msp/pages/error.php/?$1
  • 写回答

2条回答 默认 最新

  • drcmg28484 2012-01-27 03:22
    关注

    The problem is that your second RewriteRule is catching domain.com/error/access/notallowed before apache is able to make it to the third RewriteRule. So, apache says, "I've got a rule that matches, I'm finished here," and throws away the rest of your RewriteRules. If only there were a way we could tell apache to ignore the second rule in specific cases ...

    Say hello to RewriteCond

    "Wait, are you saying what I think you're saying? I can tell apache to ignore that second rule?"

    "Yes, that's exactly what I'm saying."

    RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
    RewriteCond %{REQUEST_URI} !^/error/?
    RewriteRule ^([^/.]+)/?.* index.php?$1 [PT]
    RewriteRule ^(error/.*)$ index.php?$1 [PT]
    

    See that RewriteCond line just before your second rule? You can insert any number of conditions and they will apply to the next evaluated rule. So, in this specific RewriteCond we're telling apache that the following rule shouldn't be used if the REQUEST_URI starts with /error/.

    The rewritten URL using the above rules for the address domain.com/error/access/notallowed will be:

    index.php/?error/access/notallowed

    You can adjust the regex according to your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?