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

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x