I need an htaccess rule if the url contains a question mark after the main domain

I need an htaccess rule if the url contains a question mark after the main domain

eg:

http://example.com/ ?

or

http://example.com/?xyz

it should be redirected to the main / index page

+4
source share
1 answer

Try it in root / .htaccess

RewriteEngine on

RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^/?$ /? [NC,L,R]

The empty question mark at the end of the target path is significant because it discards quignystrings orignal in apache 2.4, and later you can use the QSD flag to discard query strings.

If the above rule fails, try

RewriteEngine on
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^/?$ /? [NC,L,R]
+1
source

Source: https://habr.com/ru/post/1617836/


All Articles