重写规则重定向与得到的参数的URL

编程入门 行业动态 更新时间:2024-10-25 06:33:51
本文介绍了重写规则重定向与得到的参数的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-01 23:03:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   重定向   规则   参数   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!