问题原文
原帖链接 · 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]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
