1

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.

1
  • Are you using .htaccess?
    – MrWhite
    Feb 17, 2020 at 20:45

1 Answer 1

2
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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.