Home > Software engineering >  .htaccess redirect only homepage, not when parameter in URL
.htaccess redirect only homepage, not when parameter in URL

Time:10-13

I'm trying to forward the homepage only with a 301 redirect, that works with:

RedirectMatch 301 ^/$ https://www.site2.example/

But now I found a problem, that is when there is a parameter in the URL, then it will also forwarded to site2.example. That's not what I want.

The homepage must be forwarded to https://www.site2.example/ but only when there is no parameter in the URL, like:

www.site1.example/?s=text

Only a visit on the homepage www.site1.example/ should be forwarded to www.site2.example/.

I have tried a lot of things, but it doesn't work. For example what I have tried is

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?s=
RewriteRule ^$ https://www.site2.example/ [R=301,L]

CodePudding user response:

You want to use a condition testing for an empty query string:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?$ https://www.site2.example/ [R=301,L]

Such rule is best implemented in the http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (often called ".htaccess"), you need to enable the interpretation of such files then (see the documentation of the AllowOverride directive for that).

  • Related