有人能帮我理解一下这段代码吗?
# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? /%{REQUEST_URI}? [R=301,L]
基本上,我有一个站点www.example.com,它生成一个到www.example.com/index.cfm的链接?我需要它重定向到搜索引擎优化复制的目的www.example.com。我设法移除了index.cfm,但是?仍然留在那里(www.example.com/?)。如果是最后一个字符,尾部的斜杠也可以删除。我在网上找到了这条规则,但是在apache中我收到了一个"RewriteCond:错误的标志分隔符“警告,它什么也不做。
我还有一些像www.example.com/index.cfm?term=test这样的页面用于搜索,所以我只想去掉尾随的问号,而不是当我有一个查询附加到它的时候。
RewriteCond中出现错误。我需要帮助理解条件和为什么它不工作,而不仅仅是它的答案。
为了以防万一,这里是整个htaccess:
RewriteEngine On
Rewritebase /
# remove trailing index.cfm
RewriteRule ^index.cfm(\?)?$ / [R=301,L]
# SEF URLs
SetEnv SEF_REQUEST false
RewriteRule ^[a-z\d\-]+/[a-z]\d+/? /index.cfm/$0 [NC,PT,QSA,E=SEF_REQUEST:true]
RequestHeader add SEF-Request %{SEF_REQUEST}e
RewriteCond %{HTTP:SEF_REQUES} ^true$ [NC]
RewriteRule . - [L]
# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? ^%{REQUEST_URI}? [R=301,L]
注意:我在发帖前搜索了在线/stackoverflow,没有找到我的问题的解决方案。
编辑:我还注意到我的测试术语(\?)?$/ R=301,L正在删除index.cfm,即使它不是url中的最后一项,但当我尝试搜索某些东西(www.example.com/index.cfm?index.cfm=test)时,如果有人能纠正我并解释这将是很好的,结果是404。谢谢你。
www.example.com/index.cfm?term=test&a=dh&j=dhjsi :不应重定向EDIT2。不应重定向www.example.com/a/b/d/f/h/w/d。www.example.com/index.cfm?和www.example.com/index.cfm应该重定向到www.example.com。
发布于 2013-01-04 23:05:42
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? ^%{REQUEST_URI}? [R=301,L]
是行不通的,因为?
是正则表达式的保留字符,您需要将它与空格一起转义。尝试:
RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule ^/?(index\.cfm)? /? [R=301,L]
此外,您希望此规则在您的# remove trailing index.cfm
规则下,而不是在最底层。
发布于 2013-03-28 11:40:45
1)案例1:删除问号
http://example.com/page/subpage/?YOURSTRING=blabla
重定向到
http://example.com/page/subpage/
然后,在.htaccess的请求中,插入:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} YOURSTRING=(.*)
RewriteRule ^(.*)$ /$1? [R=301,L]
</IfModule>
# if wordpres isnot installed in root folder, then edit the fourth line to this
# RewriteRule ^(.*)$ /YOUR-WORDPRESS-DIRECTORY/$1? [R=301,L]
2)案例2:从问号重定向到另一个链接
http://example.com/index.php?YOURSTRING=blabla&id=44
重定向到
http://example.com/page/subpage/
使用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} YOURSTRING=blabla&id=44
RewriteRule ^(.*)$ http://example.com/page/subpage/? [R=301,L]
</IfModule>
https://stackoverflow.com/questions/14165287
复制相似问题