简体   繁体   English

.htaccess代码QSA用多个查询参数重写

[英].htaccess code QSA rewrite with multiple query parameters

I have my .htaccess setup to redirect event pages from /schedule/event/?q=event-name-here to /schedule/event/event-name-here 我有.htaccess设置将事件页面从/ schedule / event /?q = event-name-here重定向到/ schedule / event / event-name-这里

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^event/(.*)$ event.php?q=$1 [QSA]

However, I need to add a second query parameter for the day. 但是,我需要为当天添加第二个查询参数。 My new desired URL is: /schedule/event/friday/event-name-here I tried rewriting it as such: 我想要的新网址是:/ schedule / event / friday / event-name-这里我尝试重写它:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^event/(.*)$ event.php?day=$1&q=$2 [QSA]

But when I print_r($_GET); 但是当我print_r($ _ GET); everything is in the day parameter and q is empty. 一切都在day参数中,q为空。 $_GET['day'] is friday/event-name-here instead of being parsed into the proper parameter. $ _GET ['day']是星期五/事件名称 - 而不是被解析为正确的参数。 I didn't find documentation, but I found a similar SA post ( .htaccess rewrite rules for multiple parameters ) asking for almost the same thing. 我没有找到文档,但我发现了一个类似的SA帖子( 多个参数的.htaccess重写规则 )要求几乎相同的东西。 It looks like the same format to me. 它看起来像我一样的格式。 But mine is not working properly. 但我的工作不正常。

You need to specify two capture groups in your regular expression 您需要在正则表达式中指定两个捕获组

RewriteRule ^event/((mon|tues|wednes|thurs|fri|satur|sun)day)/(.*)$ event.php?day=$1&q=$3 [QSA]

Note the event name is matched by the 3rd capture group. 请注意,事件名称由第3个捕获组匹配。 The 2nd matches the day prefix; 第二个匹配日前缀; "mon", "tues", etc. “星期一”,“星期五”等


You could make the 2nd group more generic at the risk of matching an invalid "day name" 您可以使第二组更通用,冒着匹配无效“日期名称”的风险

RewriteRule ^event/([adefhimnorstuw]{3,6}day)/(.*)$ event.php?day=$1&q=$2 [QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM