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.
Instead of using
mod_alias
and its Redirect directive, you can use mod_rewrite using aRewriteRule
with aRewriteCond
:This redirects all requests starting with
/STRING
orSTRING
, but not starting with/STRING_PAGE
. The match for a leading slash^/
is needed if the rule is defined in aVirtualHost
context and must be omitted in a<Directory/>
or.htaccess
context. It's just easier to use/?
to make it optional, so the rule can be used in both.The NC flag is used for a case-insensitive match and L stops processing any following rules.
You need to enable
mod_rewrite
(sudo a2enmod rewrite
) to use this.