繁体   English   中英

如果使用GET参数则重写URL

[英]URL Rewrite if GET parameter

当其中有一个特殊参数时,例如/ post / path /?code = 1重定向到/ post / path /,我想将URL重定向/ RewriteRule。

我不知道,但这在这里不起作用

RewriteEngine on
RewriteBase / 
RewriteRule ^.*code=1.*$ /post/path/ [L]

如果我运行http://domain.com/post/path/?code=4/oWTYGaoOsyts,则不会有任何变化。

[REQUEST_URI] => /post/path/?code=1/oWTYGaoOsyts
[SCRIPT_NAME] => /index.php
[PHP_SELF] => /index.php

要在URL中搜索参数,您需要使用RewriteCond和%{QUERY_STRING}:

RewriteCond %{QUERY_STRING} ^.*code=1.*$
RewriteRule .* /post/path/ [L]

文档中

语法:RewriteRule模式替换[标志]

在Directory和htaccess上下文中,在删除导致服务器到达当前RewriteRule的前缀(例如,“ app1 / index.html”或“ index.html”,具体取决于指令所在的位置)之后,Pattern将首先与文件系统路径进行匹配。定义)。

如果希望与主机名,端口或查询字符串匹配,请分别使用RewriteCond和%{HTTP_HOST},%{SERVER_PORT}或%{QUERY_STRING}变量。

因为您正在搜索code=1并且您通过code=4

这应该在给定的输入下工作

RewriteRule ^.*code=4.*$ /post/path/ [L]

请注意,它可以匹配意外的网址,例如code=40somecode=4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM