RewriteRule 使用获取参数的 url 进行重定向 [英] RewriteRule to redirect with url that got parameters

查看:34
本文介绍了RewriteRule 使用获取参数的 url 进行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RewriteRule 将表单 x 页面重定向到 y 页面.x 页面有两个参数 > products.php?product=$1&page=$2 (myweb.com/products.php?prod...)y 应该是 $1/$2/(myweb.com/$1/$2/)

I am trying to use RewriteRule inorder to redirect form x page to y page. x page got two parameters > products.php?product=$1&page=$2 (myweb.com/products.php?prod...) y should be $1/$2/ (myweb.com/$1/$2/)

我试过这个>

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^products.php?product=(.*) $1/ [R=301,L]

只是为了测试,还有一些类似的代码比如> /$1/ 而不是$1/,只有$1我尝试将 RewriteCond 与 query_string 一起使用,但没有任何效果....

just for testing, and some similiar codes like > /$1/ instead $1/, only $1 I tried to use RewriteCond with query_string but nothing worked....

ofc 有继续 htaccess >

ofc that there's continue to the htaccess >

RewriteRule ^(.*)/$ $1.php [L]

还有一些不相关的规则(我认为)

and some more rules which are not relevant (I think)

你能帮我吗?

谢谢.

经过大量搜索,我找到了答案!这就是答案:

After some massive searches I found the answer! this is the answer:

RewriteCond %{QUERY_STRING} ^product=(.*)$
RewriteRule ^test.php$ %1/? [R=301,L]

现在解释一下:首先,添加将匹配查询字符串的重写条件.其次,我从重写规则和 $ = 继续(查询字符串)中删除查询字符串.我还需要添加?(问号) 在 rewriterule 的第二部分 > %1/? 问号表示我不想在新的 url 中保留查询字符串.

now for the explanation: first of all, added rewritecondition that will match the query string. second I remove the query string from the rewrite rule and the $ = the continue (the query string). I also needed to add ? (question mark) at the second part of the rewriterule > %1/? the question mark mean that I dont want to preserve the query string in the new url.

推荐答案

规则的第一部分应该包含一个正则表达式,服务器可以根据它来比较请求.例如:

The first part of your rule should contain a regular expression against which the server can compare the request. For instance:

RewriteRule ^([^/]+)/?  index.php?page=$1 [L]

这需要 http://somedomain.fake/somepage/ 并将其重定向到 http://somedomain.fake/index.php?page=somepage.规则的第一部分匹配,第二部分处理.我的规则([^/]+)"是可以匹配任何内容的正则表达式.如果您想缩小规则的工作范围,您可以自定义正则表达式以包含或排除某些字符串部分.

That would take http://somedomain.fake/somepage/ and redirect it to http://somedomain.fake/index.php?page=somepage. The first part of the rule matches, the second part handles. My rule there, "([^/]+)", is regular expression that will match anything. If you want to narrow the way your rule works, you can customize the regular expression to include or exclude certain string parts.

我建议您考虑在规则之上添加以下条件.它们确保如果匹配规则的文件或目录确实存在,服务器不会通过重写覆盖实际文件.

I suggest you consider adding the follow conditions above your rule. They ensure that if the file or directory that matches the rule actually exists, the server does not override the actual files with the rewrite.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

.htaccess 和 mod_rewrite 是伏都教,使用旧引用.在实际网站上实施此类操作之前,请务必进行彻底的研究.

.htaccess and mod_rewrite is voodoo, to use an old quote. Be sure you research thoroughly before implementing this kind of thing on a live site.

---------------------

---------------------

讨论后更新:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^product=([^/]+)$
RewriteRule ^products.php$ %1.php [L]

RewriteEngine On
RewriteCond %{QUERY_STRING} ^product=([^/]+)$
RewriteRule ^products.php$ %1/ [L]

这篇关于RewriteRule 使用获取参数的 url 进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆