【问题标题】:RedirectRule with conditions in htaccess在 htaccess 中带有条件的 RedirectRule
【发布时间】:2016-01-27 11:02:22
【问题描述】:

到目前为止我已经学会了RewriteRule ^([^/]*)/$ index.php?page=$1

问题1:如何解析url参数,因为它们在某个点之后?

例如我希望此页面 http://example.com/index.php?page=X&id=2&err=10 显示为 http://example.com/X/?id=2&err=10。鉴于参数iderr 必然不存在。他们可能会或可能不会与其他人一起出现。

问题2:对于某些特定页面假设page=Apage=B,如果id存在,也会有第三个参数。

例如我希望此页面 http://example.com/index.php?page=A&id=10&name=Abe 显示为 http://example.com/A/Abe/10/http://example.com/index.php?page=A&id=10&name=Abe&err=101 显示为 http://example.com/A/Abe/10/?err=101

16 年 1 月 30 日编辑

好的。经过大量搜索,我发现它可以通过使用{QUERY_STRING} 来实现。但是当我将RewriteRule ^([^/]*)$ index.php?page=$1&%{QUERY_STRING} [NC,L] 放在我的htaccess 中时,我的Ajax 就失控了。我希望这项工作仅适用于 index.php 而没有其他页面。

URL Rewrite GET parameters

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    使用 RewriteCond 规则我能够解决问题 1,但是,我尚未针对问题 2 实施它。

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]*)$ index.php?page=$1&%{QUERY_STRING} [NC,L]
    

    同样解决了问题 2:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^A/([^/]*)/([^/]*)/$ index.php?page=A&id=$1&z=$2&%{QUERY_STRING} [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^B/([^/]*)/([^/]*)/$ index.php?page=B&id=$1&z=$2&%{QUERY_STRING} [NC,L]
    

    注意:出于某种原因,使用此(以下)行会导致混乱。从他们那里删除评论效果很好。

    RewriteCond %{REQUEST_FILENAME} !-d # not a dir
    RewriteCond %{REQUEST_FILENAME} !-f # not a file
    RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING} [NC,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多