最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
htaccess Rewrite 301 带查询字符串(参数)
时间:2012-10-17 编辑:简简单单 来源:一聚教程网
整体url都发生了改变(域名没变),因为是lamp环境,所以想使用htccess来进行301重定向.网上找了下,有大致两种写法:
代码如下 | 复制代码 |
1.#第一种 RewriteRule a.php b.php [L,R=301] |
rewriterule方括号中为修饰符,详细的flag请看这里http://httpd.apache.org/docs/2.4/rewrite/flags.html#page-header
我的需求是把view.php?iid=123 301 到 /item/123
这种带有查询字符的链接怎么重定向呢?我尝试了以下两种但不可用:
代码如下 | 复制代码 |
|
做htaccess 中 rewrite规则时,其中301跳转,发现跳转后的网址自动加上了参数传递,也就是Querystring。
如这样的规则
代码如下 | 复制代码 |
RewriteRule ^brot.* http://local.abc.com [R=301,L] |
当遇到这样的网址时,就自动带上参数
代码如下 | 复制代码 |
http://local.abc.com/brot/dsf?abc=1 |
会变成这样的网址:
代码如下 | 复制代码 |
http://local.abc.com/?abc=1 |
后来发现,查询字符串和文件都是需要单独解析的,最终的写法为:
代码如下 | 复制代码 |
1.RewriteCond %{QUERY_STRING} ^iid=(d+)$ #如果你的apache版本>=2.4,可以使用QSD flag #RewriteRule ^view.php$ /item/%1 [L,R=301,QSD] QSD = qsdiscard 删除查询字符串(apache 2.4以后新加) |
注意事项
1.查询字符串使用的%做反向引用,正则表达式的使用和rewriterule中是一样的,%1表示除原字符串外第一个匹配,即%0为iid=123,%1为123
2.第二条中/item/%1?后面我加了个问号,这是因为我的apache版本是2.2,只有在2.4中才有flag [QSD],这里添加了个空查询,可以获得最终的item/123的结果.如果你没使用QSD或者在后面加“?”你将得到的结果是/item/123?iid=123,因为301默认把查询字符添加上了
下面是学习日记Blog全套重定向方案。供自己备忘和需要的朋友参考。
1、根目录“/”下:
代码如下 | 复制代码 |
RewriteBase /
RewriteCond % ^ab.cn [NC] RewriteRule ^(.*)$ http://www.ab.cn/$1 [L,R=301]
RewriteCond % !-f RewriteCond % !-d RewriteCond % !. .do RewriteRule . /index.PHP [L]
RewriteCond % dis(Goal|Diary)ContentAction.do RewriteCond % ^((([a-zA-Z] )=([0-9a-z]*)&)*)goalID=([0-9] )((&([a-zA-Z] )=([0-9a-z]*))*)$ RewriteRule ^dis(Goal|Diary)ContentAction.do$ http://www.ab.cn/archives/diaries/%5.htm? [R=301]
RewriteCond % ^type=latestDiaries(. )$ RewriteRule ^rssAction.do$ http://www.ab.cn/feed? [R=301]
RewriteCond % ^type=latestAdvices(. )$ RewriteRule ^rssAction.do$ http://www.ab.cn/comments/feed? [R=301]
|
2、在/archives/goals/目录下,把下面的URL重定向到/archives/diaries/下面:
代码如下 | 复制代码 |
# Control the page under /archives/goals/ 301 redirect to /archives/diaries/ # This .htaccess is puted into /archives/goals/
RewriteBase / RewriteCond % !-f RewriteCond % !-d RewriteRule ^(.*)$ http://www.ab.cn/archives/diaries/$1 [R=301]
# 2007.07.14 10:40 |
3、在目录/diaries/下,把/diaries/103.JSp形式的URL重定向到/archives/diaries/103.htm
代码如下 | 复制代码 |
# Control the page like /diaries/103.JSp 301 redirect to /archives/diaries/103.htm # This .htaccess is puted into /diaries/
RewriteBase / RewriteCond % !-f RewriteCond % !-d RewriteRule ^([0-9] ).JSp$ http://www.ab.cn/archives/diaries/$1.htm [R=301]
# 2007.07.14 11:34 |
4、在目录/goals/下,把/goals/1.JSp形式的URL重定向到/archives/diaries/1.htm
代码如下 | 复制代码 |
# Control the page like /goals/1.JSp 301 redirect to /archives/diaries/1.htm # This .htaccess is puted into /goals/
RewriteBase / RewriteCond % !-f RewriteCond % !-d RewriteRule ^([0-9] ).JSp$ http://www.ab.cn/archives/diaries/$1.htm [R=301]
# 2007.07.14 11:45 |
5、放在/java目录下,把域名java.ab.cn或www.java.ab.cn下的所有网址重定向到www.ab.cn
代码如下 | 复制代码 |
RewriteEngine on RewriteCond % ^java.ab.cn$ [OR] RewriteCond % ^www.java.ab.cn$ RewriteRule ^(.*)$ http://www.ab.cn/$1 [R=301,L] |
6、在目录/sitemaps/下,把/sitemaps/goal1-1.JSp形式的网址重定向到/archives/sitemaps/goal1-1.htm
代码如下 | 复制代码 |
# Control the page like /sitemaps/goal1-1.JSp 301 redirect to /archives/sitemaps/goal1-1.htm # This .htaccess is puted into /sitemaps/
RewriteBase / RewriteCond % !-f RewriteCond % !-d RewriteRule ^(goal[0-9] -1).JSp$ http://www.ab.cn/archives/sitemaps/$1.htm [R=301] RewriteRule ^(goals-1).JSp$ http://www.ab.cn/archives/sitemaps/$1.htm [R=301]
# 2007.07.14 12:00
|
其中,带查询字符串的重定向写法很不好找,花了我好多天时间,问了三个论坛,查了N多网页也没有结果。有朋友还下了一个结论:“除非用程序,否则不可能实现这个需求”。最后用“RewriteCond %”为关键字在google.cn上搜索,才在Module mod_rewrite:Rewriting URLs With Query Strings和mod rewrite query string problem找到一点线索,花了一个下午来调试才成功的。得来不易啊。
我对apache的mod_rewrite并不熟悉,以上有些内容自己也是从别人那里抄来的,道理还不是很明白,也许还有错误。希望发现错误和需要改进的地方的朋友提示一下
相关文章
- win11内核隔离和内存完整性介绍 10-31
- win10全屏缩放设置教程 10-31
- win10系统备份出错解决教程 10-31
- win10打开软件每次都要询问解决教程 10-31
- win10更新驱动后设备出现异常解决教程 10-31
- win10一直提示找到可能不需要的应用解决教程 10-31