I have a redirect from root to subfolder. If user visits https://example.com
it redirects to https://example.com/subfolder
. But I want it not to redirect if referrer is my site, so user can reach root page.
For example:
- User visits
https://example.com
- It redirects to
https://example.com/subfolder
- User visits
https://example.com/subfolder/file.html
- there's a link on this page to
https://example.com
and he follows it - It must open
https://example.com
and not to redirect
Here is my .htaccess:
RedirectMatch ^/$ https://example.com/
Please, give me an advice to solve the problem, I'm poor on .htaccess rules.
Best Answer
This obviously doesn't redirect to
/subfolder
as you suggest. It would create an endless redirect loop.However, you can't check the
Referer
header using a mod_aliasRedirectMatch
. You need to use mod_rewrite (or an<If>
expression) instead.For example:
Note that the check for an empty URL-path (ie.
^$
) is intentional. TheRewriteRule
pattern does not match the slash prefix.