Redirect homepage with htaccess, except if I enter the link adding “/home”

The question:

I’m trying to do something specific here, that may be a little complicated and I can’t figure out how to do it.

This is what I have with htaccess:

RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^homepageA.ar$ [OR]
RewriteCond %{HTTP_HOST} ^www.homepageA.ar$
RewriteRule ^/?$ "https://homepageB.ar/" [R=301,L]

-User enter to homepageA -> user is automatically redirected homepageB

homepageB includes a “home” menu button pointing to homepageA, so of course touching that button redirects to homepageB again. But I need to avoid this. I need that users on homepageB can go to the “old” homepageA without the redirection to the new homepageB.

Is there a way to achieve this? Or a way to append something on a link that avoids htaccess redirection (so I can modify the “home” button link accordingly)?

Thanks in advance!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

A quick way might be to simply append a query string to the URL in order to prevent the redirect (eg. ?noredirect) – simply appending a query string should not prevent the old homepageA from displaying.

You’ll presumably want to prevent indexing of the ?noredirect URL. This can be achieved by sending an X-Robots-Tag: noindex HTTP response header.

However, any user typing this URL (ie. ?noredirect) will be able to access the old homepageA, regardless of whether they have visited homepageB first.

For example:

# Redirect homePageA to homepageB, when the query string is not "noredirect"
RewriteCond %{QUERY_STRING} !^noredirect$
RewriteCond %{HTTP_HOST} ^(www.)?homepageA.ar
RewriteRule ^$ https://homepageB.ar/ [R=301,L]

# Send the X-Robots-Tag noindex header when query string is "noredirect"
RewriteCond %{QUERY_STRING} ^noredirect$
RewriteRule ^ - [E=NOINDEX:1]
Header set X-Robots-Tag "noindex" env=REDIRECT_NOINDEX

There’s no need to backslash-escape colons, slashes and dots in the RewriteRule substitution string. And the RewriteRule pattern ^$ is the same as ^/?$ when used in .htaccess.

Assuming you are on Apache and you are internally rewriting the request to the WordPress front-controller (the standard WP .htaccess directives) then you need to check REDIRECT_NOINDEX in the Header directive, despite setting the NOINDEX env var in the preceding RewriteRule directive. (The rewrite engine triggers a “loop” and the NOINDEX env var is renamed to REDIRECT_NOINDEX.)

You’ll need to clear your browser cache before testing and preferably test first with 302 (temporary) redirects before making it a 301 (permanent) redirect.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment