带动态参数的htaccess mod_rewrite

时间:2009-06-29 15:07:57

标签: .htaccess mod-rewrite

我是mod_rewrite的新手。我正在尝试将URL转发给另一个,但我无法让它工作。

说我要转发此网址: /cansas.php?m=2&id=2-0-0-0&sid=cansas to / cansas-is-good-for-you让标题以301响应,或者只是通过[R]更新URL。

我的.htaccess中有这个:

选项+ FollowSymlinks

上的RewriteEngine

RewriteRule ^ cansas.php?m = 2& id = 2-0-0-0& sid = cansas $ cansas-is-good-for-you [NC,R = 301]

我想我可以做一个简单的转发,但在某个地方停止工作。如果我删除了?m = 2& id = etc,它只将cansas部分转发到新部分,所以它看起来像这样:cansas-is-good-for-you?m = 2& id = 2-0- 0-0&安培; SID = cansas。

当我在URL字符串中有多个动态参数时,如何转发它?我需要转发的页面上的示例:

/cansas.php?m=2&id=2-0-0-0&sid=cansas

/cansas.php?m=2&id=2-1-0-0&sid=cansas

/cansas.php?m=2&id=2-2-0-0&sid=cansas

任何帮助都将非常感谢:)

也许不可能这样做?我现在设置的方式是我想使用名为/ cansas-is-good-for-you的新URL,它从源代码中读取/cansas.php?m=2&id=2-0-0 -0& sid = cansas,但浏览器中显示的URL应为:/ cansas-is-good-for-you。我需要转发旧的cansas.php吗?新网址的URL:)

1 个答案:

答案 0 :(得分:1)

您需要使用RewriteCond指令检查URL的查询,因为RewriteRule指令仅处理URL路径:

RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

如果您只想检查一个参数,请使用:

RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

要仅针对初始请求执行此操作,您需要检查request line

RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]