Home > Software engineering >  .htaccess redirect Home page to query string
.htaccess redirect Home page to query string

Time:02-13

I need to redirect

example.com/

to new path on same domain:

example.com/?viewr=UNESCO/$1

How can do it using .htaccess?

Any help would be appreciated.

CodePudding user response:

To redirect example.com/ to example.com/?viewr=UNESCO/$1 then you would need to use mod_rewrite in order to check the query string. Try the following at the top of root .htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ /?viewr=UNESCO/\$1 [R=302,L]

The literal $ in the substitution string (2nd argument to the RewriteRule directive) needs to be backslash-escaped to prevent $1 being interpreted as a backreference (which is empty).


UPDATE: If you are using this in a virtualhost context (ie. directly in the <VirtualHost> container, not inside a <Directory> container) then you will need to adjust the RewriteRule pattern from ^$ to ^/$. In other words:

:
RewriteRule ^/$ /?viewr=UNESCO/\$1 [R=302,L]

Reference:

  • Related