Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess check if empty query string

I have the following situations on my server:

/->
  news-> 
    .htaccess
    index.php
    post.php
...

And the following rules in my .htaccess:

RewriteRule    ^(.*)/admin          post.php?slug=$1&admin_mode=true            [NC,QSA,L]
RewriteRule    ^(.*)$               post.php?slug=$1                            [NC,QSA,L]

Now I need my URLs to be the following:

If requested www.mydomain.com/news/ -> it should get the index.php file

If requested www.mydomain.com/friendly-title-of-my-article -> it should get the post.php file with the query string as indicated in my .htaccess.

Currently I get correctly the post.php with the query string, but when I go to www.mydomain.com/news/ , it's requesting the post.php file.

Please help. Thanks

like image 276
somonek Avatar asked Sep 13 '25 15:09

somonek


1 Answers

Use this

#if query string is empty

RewriteCond %{QUERY_STRING} ^$

#match on / for root, or .index.php in request and send to query string version 

RewriteRule ^(/|index.php)?$ /index.php?step=1 [R=301,L]
like image 135
Ninju Avatar answered Sep 16 '25 06:09

Ninju