【问题标题】:Default Query String in .htaccess.htaccess 中的默认查询字符串
【发布时间】:2013-01-15 13:40:51
【问题描述】:

有点奇怪的问题,但我想在我的所有 URL 上设置一个查询字符串。如果参数未设置(或为空),那么我想重定向以包含默认值。

例如:

example.com would need to requrect to example.com?param=a

example.com?param would also need to redirect to example.com?param=a

如果参数已设置并且是已知值列表的一部分,那么它应该照常进行:

example.com?param=(a|b|c|d) would go to the respective page a,b,c or d

站点的某些页面使用其他参数进行排序和分页,因此规则不能假定这是唯一的查询字符串。

我尝试了几件事,但一直卡在重定向循环中。这是尝试设置默认参数:

RewriteCond %{QUERY_STRING} !(^|&)param=(a|b|c|d)($|&)
RewriteRule ^(.*)$ /index.php?rq=$1&param=a [L,QSA]

主要的CMS重写规则是:

RewriteCond %{REQUEST_URI} !^\/*(index\.php|blog|admin\/assets|site\/assets|robots.txt|sitemap(|\-[0-9]+)\.xml|products.xml|favicon\.ico)
RewriteRule ^(.*)$ /index.php?rq=$1 [L,QSA]

任何帮助都会很棒!

【问题讨论】:

    标签: .htaccess url-rewriting


    【解决方案1】:
    RewriteCond %{QUERY_STRING} !(^|&)param=(a|b|c|d)($|&)
    RewriteCond %{REQUEST_URI} !^\/*(index\.php|blog|admin\/assets|site\/assets|robots.txt|sitemap(|\-[0-9]+)\.xml|products.xml|favicon\.ico)
    RewriteRule ^(.*)$ $1?%{QUERY_STRING}&param=a [L]
    
    RewriteCond %{REQUEST_URI} !^\/*(index\.php|blog|admin\/assets|site\/assets|robots.txt|sitemap(|\-[0-9]+)\.xml|products.xml|favicon\.ico)
    RewriteRule ^(.*)$ /index.php?rq=$1 [L,QSA]
    

    【讨论】:

    • 谢谢,快到了。我必须更改第一条规则以读取 RewriteRule ^(.*)$ /index.php?rq=$1&%{QUERY_STRING}&param=a [L] 如果未设置参数,我需要将其附加到地址栏中的 URL。这可能吗?
    • 将 [L] 更改为 [L,R=301](但在测试时使用 302)。
    • 谢谢,这是添加参数,但我需要隐藏 ?rq= 部分。基本上,我希望它重写 URL,但只需附加 ?/&param= 部分。这甚至可能吗?
    • 您应该将其添加到我提供的规则中。你能告诉它是什么不工作吗? (因为你必须改变它)
    • 我的错误 - 你的代码很好。我遇到的问题是我一直需要 URL 中的参数 - 它需要将自身(一旦设置)附加到页面上的每个链接,以便每次都传递值。我想我应该提出一个新问题。感谢您的帮助
    猜你喜欢
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多