2012-12-30 343 views
0

我有两个网址,我正在尝试重写,过去... 4-5小时(现在头痛)。Htaccess将GET请求重写为另一个GET请求,另一个GET GET请求使用2个GET变量

我试图改写

/arts/tag/?tag=keyword 

/search/art?keywords=keyword 

看着其他的问题,我制定了我重写这样

RewriteRule /arts/tag/?tag=([^&]+) search/art?keywords=$1 [L,R=301,NC] 

RewriteRule ^arts/tag/?tag=$ /search/art\?keywords=%1? [L,R=301,NC] 

我尝试了反斜杠,没有,没有运气。

也试过

RewriteCond %{QUERY_STRING} /arts/tag/?tag=([^&]+) [NC] 
RewriteRule .* /search/art\?keywords=%1? [L,R=301,NC] 

第二个是类似的,

/arts/category?id=1&sortby=views&featured=1 

/art/moved?id=1&rearrange=view 

我改变得变量名是我自己学习的原因目的,因为我没有找到任何教程为我的目的。由于类别已更改,我还将类别更改为移动,并且我必须在内部重定向某些ID#。

RewriteCond %{QUERY_STRING} id=([^&]+) [NC] // I need the path in there though, not just query string, since I'll be redirecting /blogs/category and /art/category to different places. 
RewriteRule .* /art/moved/id=%1? [L,R=301,NC] 

任何帮助将不胜感激。谢谢。

+0

当追加到查询字符串时,应该使用'QSA'作为'RewriteRule'的标志(QSA代表“Query String Append”)。 – fge

+0

谢谢,我会给它一个镜头。 – Darius

+0

像这样? RewriteRule/arts/tag /?tag = $/search/art?keywords = $ 1 [QSA,NC],仍然没有。我试着在上面发布的所有代码中实施QSA,而不仅仅是那一个。 – Darius

回答

1

假设在原来的网址查询没有任何共同之处与那些在替代网址,也许这将做你想做的,使用在查询中的第key为条件,并确定输入的URL:

RewriteEngine On 
RewriteBase/
# First case 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} \btag\b 
RewriteRule .* http://example.com/search/art?keywords=keyword? [L] 

将映射这样的: http://example.com/arts/tag/?tag=keyword

要这样: http://example.com/search/art?keywords=keyword

# Second case 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} \bid\b 
RewriteRule .* http://example.com/art/moved?id=1&rearrange=view? [L] 

将映射这样的: http://example.com/arts/category?id=1&sortby=views&featured=1

要这样: http://example.com/art/moved?id=1&rearrange=view

两者都默默的映射。如果新的URL将被显示在浏览器的地址栏中,请修改像这样的标志[R,L]。将R替换为R=301以获得永久重定向。

+0

谢谢你。对于第一个,如果我专门为/艺术重写,并且不想/视频重写,我将如何陈述?如果关键字是动态的,我会使用$ 1还是%1,“keyword”是动态的。所以要重申一下,我只想在相同的条件下重写/ in/arts,而不是/ videos或/ blog。 第二种情况也是如此。 谢谢。这工作,只需要看看它是如何动态的。 – Darius

+0

因此,您希望第一个'key'在各个URL中是动态的,只有'/ arts'和'/ category',否则其他任何内容都应该被拒绝。我对吗?将等待您的评论来更新我的答案。 –

+0

我想/ VIDEOS/tag /?tag =关键字保持不变,但是如果是/ ARTS/tag /?tag =关键字来处理重写。此外,“关键字”一词是动态的,所以在重写时,如果url是/ arts/tag /?tag = hello它应该重写search/art?keywords = hello关键字是动态变量。对于第二个查询,同样的事情,GET变量在重写时应该是动态的。非常感谢您的帮助。你是最棒的! – Darius