删除尾随的问号htaccess的 [英] Removing trailing question mark with htaccess
问题描述
有人可以帮助我了解这个code?
#删除尾随?
的RewriteCond%{} THE_REQUEST? HTTP [NC]
重写规则? /%{REQUEST_URI}? [R = 301,L]
基本上我有一个网站www.example.com正在生成一个链接到www.example.com/index.cfm~~V?我需要它重定向到www.example.com的搜索引擎优化复制的目的。我设法去除index.cfm但?还停留在那里(www.example.com/?)。如果它是最后一个字符的斜线也被删除就好了。我发现这条规则在网上,但我得到一个的RewriteCond:坏标志符。警报在Apache中,并没有做任何事情。
我也有一些网页一样www.example.com/index.cfm?term=test用于搜索,所以我只是想摆脱尾随的问号,而不是当我有连接到它的查询。
该错误是在RewriteCond指令。我需要帮助了解情况,为什么它不工作,不只是它的答案。
以防万一这里是整个htaccess的:
RewriteEngine叙述上
的RewriteBase /
#删除尾随index.cfm
重写规则^ index.cfm(\?)?$ / [R = 301,L]
#海基会网址
SETENV SEF_REQUEST假
重写规则^ [A-Z \ D \ - ] + / [A-Z] \ D + /? /index.cfm/$0 [NC,PT,QSA,E = SEF_REQUEST:真]
RequestHeader添加海基会请求%{SEF_REQUEST} e所
的RewriteCond%{HTTP:SEF_REQUES} ^真$ [NC]
重写规则。 - [L]
#删除尾随?
的RewriteCond%{} THE_REQUEST? HTTP [NC]
重写规则? ^%{REQUEST_URI}? [R = 301,L]
注:我张贴,并没有找到一个解决我的问题之前,没有在网上搜索/计算器
编辑:另外,我发现我重写规则^ index.cfm(\?)$ / [R = 301,L]被拆除index.cfm即使它不是导致404时的最后一件事的网址是什么?我尝试搜索的东西(www.example.com/index.cfm?term=test)如果有人可以纠正我,并解释说,将是巨大的。谢谢你。
EDIT2:www.example.com/index.cfm?term=test&a=dh&j=dhjsi不应该被重定向。 www.example.com/a/b/d/f/h/w/d不应被重定向。 www.example.com/index.cfm~~V?和www.example.com/index.cfm应该被重定向到www.example.com。
的RewriteCond%{} THE_REQUEST? HTTP [NC]
重写规则? ^%{REQUEST_URI}? [R = 301,L]
是行不通的,因为?
是正规EX pressions保留字符,你就需要逃避它随着空间。尝试:
的RewriteCond%{THE_REQUEST} \?\ HTTP [NC]
重写规则^ /?(指标\ .CFM)? /? [R = 301,L]
此外,你要根据本条规则你的#删除尾随index.cfm
规则,而不是在最底层。
Can someone help me understand this code?
# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? /%{REQUEST_URI}? [R=301,L]
Basically I have a site www.example.com that is generating a link to www.example.com/index.cfm? I need it to redirect to www.example.com for SEO duplication purposes. I managed to remove the index.cfm but the ? still stays there (www.example.com/?). The trailing slash is also removed just fine if it's the last character. I found this rule online but I'm getting a "RewriteCond: bad flag delimiters" alert in apache and it doesn't do anything.
I also have some pages like www.example.com/index.cfm?term=test for searching so I just want to get rid of the trailing question mark and not when I do have a query attached to it.
The error is in the RewriteCond. I need help understanding the condition and why it doesnt work not just the answer to it.
Just in case here is the entire 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]
NOTE: I did search online/stackoverflow before posting and did not find a solution to my problem.
EDIT: Also I noticed that my RewriteRule ^index.cfm(\?)?$ / [R=301,L] is removing the index.cfm even if it's not the last thing in the url resulting in a 404 when i try searching something (www.example.com/index.cfm?term=test) If someone could correct me and EXPLAIN that would be great. Thanks you.
EDIT2: www.example.com/index.cfm?term=test&a=dh&j=dhjsi should NOT be redirected. www.example.com/a/b/d/f/h/w/d should not be redirected. www.example.com/index.cfm? and www.example.com/index.cfm should be redirected to www.example.com.
RewriteCond %{THE_REQUEST} ? HTTP [NC]
RewriteRule .? ^%{REQUEST_URI}? [R=301,L]
Isn't going to work, because ?
is a reserved character for regular expressions and you'd need to escape it along with the space. Try:
RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule ^/?(index\.cfm)? /? [R=301,L]
Additionally, you want this rule under your # remove trailing index.cfm
rule, and not at the very bottom.
这篇关于删除尾随的问号htaccess的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!