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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog