Search code examples
apache.htaccessmod-rewriteurl-rewritingquery-string

RewriteRule gives me unwanted $_GET param


I need video/5 to be video.php?id=5

RewriteRule ^video$ video.php [L]
RewriteRule ^video/([^/]*)/?$ video.php?id=$1 [QSA,NC,L]

esentially - the above code works - but here is a problem:

video/ - without id - gives me an empty string as $_GET['id']

so if I write:

if(isset($_GET['id']))... - the result is true and the value is ""

how to avoid this ?

I want just video/ - without any $_GET variable - except it is set explicitly - for example - video/5


Solution

  • With your shown samples, please try following htaccess rules. Make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##Internal rewrite to video.php without $1 in it.
    RewriteRule ^video/?$ video.php [NC,L]
    
    ##Rules for internal rewrite with passing id query string to it.
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^video/([^/]*)/?$ video.php?id=$1 [QSA,NC,L]