使用 htaccess 删除尾随问号 [英] Removing trailing question mark with htaccess

查看:28
本文介绍了使用 htaccess 删除尾随问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮我理解这段代码吗?

Can someone help me understand this code?

# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC] 
RewriteRule .? /%{REQUEST_URI}? [R=301,L]

基本上我有一个网站 www.example.com 生成一个链接到 www.example.com/index.cfm?我需要它重定向到 www.example.com 以进行 SEO 复制.我设法删除了 index.cfm 但 ?仍然留在那里(www.example.com/?).如果结尾的斜杠是最后一个字符,它也会被删除.我在网上找到了这条规则,但我在 apache 中收到了RewriteCond: bad flag delimiters"警报,但它没有做任何事情.

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.

我也有一些页面,如 www.example.com/index.cfm?term=test 用于搜索,所以我只想去掉尾随的问号,而不是当我有附加查询时.

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.

错误在 RewriteCond 中.我需要帮助了解这种情况以及为什么它不起作用,而不仅仅是它的答案.

The error is in the RewriteCond. I need help understanding the condition and why it doesnt work not just the answer to it.

以防万一这里是整个 htaccess:

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-zd-]+/[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 上搜索过,但没有找到解决我的问题的方法.

NOTE: I did search online/stackoverflow before posting and did not find a solution to my problem.

我还注意到我的 RewriteRule ^index.cfm(?)?$/[R=301,L] 正在删除 index.cfm,即使它不是 url 中的最后一件事,导致 404我试着搜索一些东西(www.example.com/index.cfm?term=test)如果有人能纠正我并解释那会很棒.谢谢你.

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.

不应重定向 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?和 www.example.com/index.cfm 应该被重定向到 www.example.com.

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]

此外,您希望此规则在 # remove trailing index.cfm 规则下,而不是在最底部.

Additionally, you want this rule under your # remove trailing index.cfm rule, and not at the very bottom.

这篇关于使用 htaccess 删除尾随问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆