大网数据广告活动
网站运营与故障问答

Apache, Redirect is not working for substring

· 52CE 我爱测

Issue In apache, how can I redirect to homepage in case if you will go to exact string, but everything behind this string should be displayed normally, no redirect. Example. http://example.com/STRING -> go to homepage http://exam…

问题原文

原帖链接 · genderbee · CC BY-SA 4.0

Issue

In apache, how can I redirect to homepage in case if you will go to exact string, but everything behind this string should be displayed normally, no redirect.

Example.

http://example.com/STRING -> go to homepage
http://example.com/STRING_PAGE -> go to http://example.com/STRING_PAGE

I tried

Redirect 301 /STRING http://example.com/

But in this case page is redirected to homepage too, for example in case of http://example.com/STRING_PAGE.

Thanks.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Redirect 301 /STRING http://example.com/

The mod_alias Redirect directive is prefix-matching, so the above will match requests for both /STRING and /STRING_PAGE.

You need to use RedirectMatch (also part of mod_alias) if you want an exact match - this uses regular expressions to match against the requested URL.

For example, to match /STRING only then try the following instead:

RedirectMatch 301 ^/STRING$ http://example.com/

(You will need to clear your browser cache before testing.)

However, if you are already using mod_rewrite (ie. RewriteRule) for other rewrites/redirects then you may need to use mod_rewrite instead in order to avoid conflicts. For example, the equivalent using mod_rewrite would be:

RewriteEngine On

RewriteRule ^/?STRING$ http://example.com/ [R=301,L]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2020-02-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。