optional url parameters and mod_rewrite

Discussion in 'Apache' started by zibinet, Feb 28, 2008.

  1. #1
    Hello,

    Im relatively new to using mod_rewrite and regular expressions as well. I am SEOing my links and using the following rewrite rule:

    RewriteRule ^.*_deal([0-9]+)(_more.*)?$ pddetail.php?id=$1&page=$2 [NC,L]

    The thing is though, the page parameter ($2) is optional, so if the URL coming in doesnt have this variable: (_more.*), I dont want the page parameter to exist. With the above rule, if (_more.*) is empty, it just passes an empty page parameter.

    Any ideas on how I can avoid this and not pass the page parameter if (_more.*) is empty?

    Thanks.
    zibinet
     
    zibinet, Feb 28, 2008 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    Add a rule above that one which applies to only the first variable existing, the [L]ast flag on this new rule will stop the one you have there from being processed if _more doesn't exist.
    RewriteRule ^.*_deal([0-9]+)$ pddetail.php?id=$1 [NC,L]
    RewriteRule ^.*_deal([0-9]+)_more(.*)$ pddetail.php?id=$1&page=$2 [NC,L]
    Code (markup):
     
    joebert, Feb 28, 2008 IP