重写规则重定向与得到的参数的URL [英] RewriteRule to redirect with url that got parameters

查看:278
本文介绍了重写规则重定向与得到的参数的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用重写规则序重定向形如x页为y页。 XX扫描页有两个参数> products.php产品= $ 1安培;页= $ 2(myweb.com/products.php?prod ...) Ÿ应该是$ 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]

只是用于测试,以及一些similiar codeS一样> / $ 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]

现在的解释: 首先,补充rewritecondition将匹配查询字符串。 第二个我从重写规则和$删除查询字符串=的继续(查询字符串)。我还需要补充的吗? (问号)的重写规则的第二部分> %1 /?问号的意思是,我不想preserve在新的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.

推荐答案

你的规则是否应该定期EX pression针对的服务器可以比较请求的第一部分。例如:

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页= SomePage的。该规则的匹配的第一部分,第二部分处理。我的规则存在,([^ /] +),是正规的前pression将匹配任何东西。如果要缩小规则的工作方式,你可以自定义的普通恩pression包含或排除某些字符串的部分。

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]

这篇关于重写规则重定向与得到的参数的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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