htaccess redirect only if no parameters are provided

27 Views Asked by At

I have a website like www.website.com/index.php which is the homepage. I select the different product with several parameters, example www.website.com/index.php?id=xxx or www.website.com/index.php?cat=xxxx

I have several lines in my htaccess to redirect the query. For example, for the products I use the following code:

RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /product/%1? [R=301,L,NE]
RewriteRule ^product/([\w-]+)/?$ index.php?id=$1 [L,QSA,NC]

that works fine.

Now I would like to redirect to the domain root the requests to index.php that do not have any parameters, so www.website.com/index.php should become www.website.com. Any other query should not be affected. How can I do it ?

1

There are 1 best solutions below

0
CBroe On BEST ANSWER
# test that the query string is empty
RewriteCond %{QUERY_STRING} ^$ 
RewriteRule ^index\.php$ / [R=301]

That should do the job.