Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

How do i make a htaccess variable optional?


How do i make that the third passed 'get' variable on first line is not mandatory? In other words, page only works if there are 3 'get' variables given, but in my case, i need the third one (e) to be optional. Any ideas?

RewriteRule ^ft/([0-9]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ forum.php?ft=$1&seo=$2&e=$3 [NC,QSA,L]
RewriteRule ^(fc|ft)/([0-9]+)/?$ forum.php?$1=$2 [NC,QSA,L]
RewriteRule ^forums/?$ forum.php [NC,L]

Solution

  • In that case to handle both 2 and 3 variables, try adding 1 more rules in your htaccess Rules file, have your htaccess rules file in following manner.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##Newly added rewrite Rule to handle 2 parameters in uri.
    RewriteRule ^ft/([0-9]+)/([A-Za-z0-9-]+)/?$  forum.php?ft=$1&seo=$2 [NC,QSA,L]
    
    ##Rest of the rules go from here onwards....
    RewriteRule ^ft/([0-9]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ forum.php?ft=$1&seo=$2&e=$3 [NC,QSA,L]
    RewriteRule ^(fc|ft)/([0-9]+)/?$ forum.php?$1=$2 [NC,QSA,L]
    RewriteRule ^forums/?$ forum.php [NC,L]