本文 31769 pv
2
A地址:域名/ProductView.jsp?lClassID=200
B地址:域名/goods.php?id=3
实现把用户输A地址跳到B地址,就是跳转的功能。
原先用我用最常的方法实现如:RewriteRule ^ProductView.jsp?lClassID=200$ goods.php?id=3 [L]
看上去是没有问题的,但在地址上输入跳转不了。后来网上查了一下资料如下:
规则:
引用自
文本
. 任意一个单字符
[chars] 字符类: "chars"中的任意一个字符
[^chars] 字符类: 不在"chars"中的字符
text1|text2 选择: text1 或 text2
量词
? 前面的字符出现 0 或 1 次
* 前面的字符出现 0 或 N 次(N > 0)
+ 前面的字符出现 1 或 N 次(N > 1
原来这样:?号把前面的p也作为参数了,p?(\?)就变成了$1,当在地址中输入http://www.geekso.com/ProductView.jslClassID=200 可实现跳转,但这显然不是需求那样的.
本来都想放弃用重写了,在网上又找了一下终于找到了,功夫不负有心人啊,
解决方法如下:
RewriteCond %{QUERY_STRING} ^lClassID=200$
RewriteRule ^ProductView\.jsp$ goods\.php\?id=3 [L]
我找到的相关资料:
把 /abc?id=123 => /def.php?id=123 的写法:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.+)$
RewriteRule ^/abc$ /def.php?sid=%1 [L]
RewriteRule 不会去匹配 ? 后面的字符串,需要用RewriteCond来匹配
参考:http://lists.apple.com/archives/web-dev/2006/Mar/msg00005.html
附件:
1楼:
Options +FollowSymlinks
RewriteEngine on
# Set the RewriteBase to "/" for the root level to base everything on... I guess.
RewriteBase /
RewriteRule ^oldpage.php(.+) newpage.php$1&otherid=0 [R]
-------------------------------------------------
The $1 should have all the variable stuff in it from the php.
So I'm looking for it to essentially do this:
oldpage.php?id=1 redirects to newpage.php?id=1&otherid=0
Why is this not working? Any help would be appreciated.
2楼:
Because the RewriteRule directive doesn't parse anything beyond the path portion of the requested URL. If you want to capture the query string, you have to use a RewriteCond statement to store the query string in a regex back-reference. You can then make use of the RewriteCond's back-references in a succeeding RewriteRule statement. There's actually a shortcut for doing this (check the QSA flag in the mod_rewrite docs), but you need to modify the query string in the rewritten URL, so you can't use that.
Give this a try:
RewriteCond %{QUERY_STRING} ^(.+)$RewriteRule ^oldpage.php newpage.php?otherid=0&%1 [R]
-Brian
3楼:So I fiddled around and came up with this: RewriteRule ^oldpage.php newpage.php?otherid=0 [QSA,R] Why does this work? The only thing I can figure is that the query string is simply added to the end (as is says in the docs) of the new page. But I'm not even referencing the query string in the first part. Do I not need to do that? Does QSA take whatever the query
4楼:Hey,mod_rewrite is the swiss army knife of web deployment, so how could this beOT? ;-) Firstly, you're missing a question mark in your rewrite rule, every requestwill at least start with a SLASH (/) and then adding QSA to your rewriterule might help in passing over query strings.(QSA = QeryStringAppend) ^ matches the begining of the requested URI which will always start with aslash as in: http//www.apple.com/hardware^ protocol^ host^ requested URI including / So give this a try: RewriteRule ^/oldpage.php(.*) newpage.php?$1&otherid=0 [QSA,R]^ ^ ^^^this is needed you missed this this QSA is new or evenRewriteRule ^/oldpage.php(.*) newpage.php?$1&otherid=0 [QSA,R=301](R=301 makes the redirect a PERMANENT redirect) I'd even go so far to add a Rewrite Condition, so the rule doesn't have tobe run for all requests: RewriteCond %{REQUEST_URI} ^/.*\.php.* [NC]RewriteRule ^oldpage.php(.*) newpage.php?$1&otherid=0 [QSA,R=301] ([NC] turns on case insensitivenes on the matches so .PHP=.php etc.) Also to debug problems with ModRewrite, it realy helps turning logging on tosee what is actualy happening: RewriteLog "/var/log/httpd/rewrite.log"RewriteLogLevel 13
Apache Rewrite解决问号匹配的写法
© kekehu / 技术资源 / 2008.06.24 / 17:11 / 31769PV
这个问题我用了一个上午终于解决了,需求是这样的:A地址:域名/ProductView.jsp?lClassID=200
B地址:域名/goods.php?id=3
实现把用户输A地址跳到B地址,就是跳转的功能。
原先用我用最常的方法实现如:RewriteRule ^ProductView.jsp?lClassID=200$ goods.php?id=3 [L]
看上去是没有问题的,但在地址上输入跳转不了。后来网上查了一下资料如下:
规则:
引用自
文本
. 任意一个单字符
[chars] 字符类: "chars"中的任意一个字符
[^chars] 字符类: 不在"chars"中的字符
text1|text2 选择: text1 或 text2
量词
? 前面的字符出现 0 或 1 次
* 前面的字符出现 0 或 N 次(N > 0)
+ 前面的字符出现 1 或 N 次(N > 1
原来这样:?号把前面的p也作为参数了,p?(\?)就变成了$1,当在地址中输入http://www.geekso.com/ProductView.jslClassID=200 可实现跳转,但这显然不是需求那样的.
本来都想放弃用重写了,在网上又找了一下终于找到了,功夫不负有心人啊,
解决方法如下:
RewriteCond %{QUERY_STRING} ^lClassID=200$
RewriteRule ^ProductView\.jsp$ goods\.php\?id=3 [L]
我找到的相关资料:
把 /abc?id=123 => /def.php?id=123 的写法:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.+)$
RewriteRule ^/abc$ /def.php?sid=%1 [L]
RewriteRule 不会去匹配 ? 后面的字符串,需要用RewriteCond来匹配
参考:http://lists.apple.com/archives/web-dev/2006/Mar/msg00005.html
附件:
1楼:
Options +FollowSymlinks
RewriteEngine on
# Set the RewriteBase to "/" for the root level to base everything on... I guess.
RewriteBase /
RewriteRule ^oldpage.php(.+) newpage.php$1&otherid=0 [R]
-------------------------------------------------
The $1 should have all the variable stuff in it from the php.
So I'm looking for it to essentially do this:
oldpage.php?id=1 redirects to newpage.php?id=1&otherid=0
Why is this not working? Any help would be appreciated.
2楼:
Because the RewriteRule directive doesn't parse anything beyond the path portion of the requested URL. If you want to capture the query string, you have to use a RewriteCond statement to store the query string in a regex back-reference. You can then make use of the RewriteCond's back-references in a succeeding RewriteRule statement. There's actually a shortcut for doing this (check the QSA flag in the mod_rewrite docs), but you need to modify the query string in the rewritten URL, so you can't use that.
Give this a try:
RewriteCond %{QUERY_STRING} ^(.+)$RewriteRule ^oldpage.php newpage.php?otherid=0&%1 [R]
-Brian
3楼:So I fiddled around and came up with this: RewriteRule ^oldpage.php newpage.php?otherid=0 [QSA,R] Why does this work? The only thing I can figure is that the query string is simply added to the end (as is says in the docs) of the new page. But I'm not even referencing the query string in the first part. Do I not need to do that? Does QSA take whatever the query
4楼:Hey,mod_rewrite is the swiss army knife of web deployment, so how could this beOT? ;-) Firstly, you're missing a question mark in your rewrite rule, every requestwill at least start with a SLASH (/) and then adding QSA to your rewriterule might help in passing over query strings.(QSA = QeryStringAppend) ^ matches the begining of the requested URI which will always start with aslash as in: http//www.apple.com/hardware^ protocol^ host^ requested URI including / So give this a try: RewriteRule ^/oldpage.php(.*) newpage.php?$1&otherid=0 [QSA,R]^ ^ ^^^this is needed you missed this this QSA is new or evenRewriteRule ^/oldpage.php(.*) newpage.php?$1&otherid=0 [QSA,R=301](R=301 makes the redirect a PERMANENT redirect) I'd even go so far to add a Rewrite Condition, so the rule doesn't have tobe run for all requests: RewriteCond %{REQUEST_URI} ^/.*\.php.* [NC]RewriteRule ^oldpage.php(.*) newpage.php?$1&otherid=0 [QSA,R=301] ([NC] turns on case insensitivenes on the matches so .PHP=.php etc.) Also to debug problems with ModRewrite, it realy helps turning logging on tosee what is actualy happening: RewriteLog "/var/log/httpd/rewrite.log"RewriteLogLevel 13
2018.03.20 17:05
不让复制真的太。。。
2008.07.08 10:11
我现在非常痛苦,也是遇到了这个问题。
prop/loadProp.do?cityId=$1&id=$2
用正则表达式获取$1和$2
谁帮忙写一下
谢谢了
需要让显示成
prop/$1$2.html
这样的
不知道应该怎么写cond