[英]Htaccess Rewrite Rule Conditional
I am a complete noob when it comes to the .htaccess file... What I'm trying to do is check to see if a GET variable exists, ?si=10
, and pass that onto the rewrite, but only if it exists.当谈到 .htaccess 文件时,我是一个完全的菜鸟......我要做的是检查 GET 变量是否存在, ?si=10
,并将其传递给重写,但前提是它存在。
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1
This way when I have say website.com/user/Username/
it goes to, on the server, website.com/profile/?name=Username
instead.这样,当我说website.com/user/Username/
时,它会在服务器上转到website.com/profile/?name=Username
。 But when I add do website.com/user/Account/?si=10
, the server isn't passing the si
variable onto the actually loaded page.但是当我添加 do website.com/user/Account/?si=10
时,服务器没有将si
变量传递到实际加载的页面上。 Any idea how I would do this?知道我会怎么做吗? Sorry if I worded this badly..对不起,如果我措辞不好..
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1 [QSA]
From the manual...从手册...
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one.当替换 URI 包含查询字符串时,RewriteRule 的默认行为是丢弃现有的查询字符串,并将其替换为新生成的查询字符串。 Using the [QSA] flag causes the query strings to be combined.使用 [QSA] 标志会合并查询字符串。
You can do it like this:你可以这样做:
RewriteCond %{QUERY_STRING} ^si=([0-9]+)$ // replace regex as neccesary
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1&si=%1
The percentage sign % brings in the match or matches from the rewriteCond(s) and you use the dollar sign as normal for the matches from the rewriteRule.百分号 % 从 rewriteCond(s) 中引入一个或多个匹配项,您可以正常使用美元符号来处理来自 rewriteRule 的匹配项。 This gives you full control of your query vars, but yes, QSA is simpler.这使您可以完全控制查询变量,但是是的,QSA 更简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.