0

I have a frindly(not so much but fits me well) URL expression on my HTACCESS. The page treats accordly with "mode" and ids

For editing a category:

#URL
    edit.php?mode=cat&cat=1
#The expression
    RewriteRule ^editar/([^/]*)/([^/]*)$ /edit.php?mode=$1&cat=$2 [NC,L]
#Output
    /editar/cat/1

Editing subcategory:

#URL
    edit.php?mode=subcat&cat=1&subcat=1
#The expression
    RewriteRule ^editar/([^/]*)/([^/]*)/([^/]*)$ /edit.php?mode=$1&cat=$2&subcat=$3 [NC,L]
#Output
    /editar/subcat/1/1

Editing product:

#URL
    edit.php?mode=prod&cat=1&subcat=1&prod=1
#The expression
    RewriteRule ^editar/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /edit.php?mode=$1&cat=$2&subcat=$3&prod=$4 [NC,L]
#Output
    /editar/prod/1/1/1

So far, so good!

But I have three expressions for the same page.

RewriteRule ^editar/([^/]*)/([^/]*)$ /edit.php?mode=$1&cat=$2 [NC,L]
RewriteRule ^editar/([^/]*)/([^/]*)/([^/]*)$ /edit.php?mode=$1&cat=$2&subcat=$3 [NC,L]
RewriteRule ^editar/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /edit.php?mode=$1&cat=$2&subcat=$3&prod=$4 [NC,L]

Is there any way to reduce that to just ONE expression, where some varible might be or might no be present on the string?

Like: edit.php?mode=prod&cat=1 THIS -> &subcat=1 AND THIS -> &prod=1 being optional?

Thanks!

kold-kreator
  • 179
  • 2
  • 2
  • 12

2 Answers2

1

Try this :

RewriteEngine On
RewriteRule ^editar/([^/]+)/([^/]+)/?([^/]*)/?([^/]*)?$  - [E=PASS:mode=$1&cat=$2&subcat=$3&prod=$4]
RewriteCond %{ENV:PASS} ^((mode=(.*))&subcat=&prod=|(mode=(.*)&subcat=(.+))&prod=|(mode=(.*)&subcat=(.+)&prod=(.+)))$
RewriteRule ^    /edit.php?%2%4%7 [L,NE]

So , with the code above you will be able to handle requests like the following :

/editar/1 or editar/1/     = not match
/editar/1/2 or editar/1/2/   = match and redirect to /edit.php?mode=1&cat=2
/editar/1/2/3 or editar/1/2/3/   = match and redirect to /edit.php?mode=1&cat=2&subcat=3
/editar/1/2/3/4  = match and redirect to /edit.php?mode=1&cat=2&subcat=3&prod=4
/editar/1/2/3/4/    = not match

Note: clear browser cache then test it .

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • Worked very well, Mohammed. Thank you very much! Can you suggest some read about these regex or could give me a brief explain about it?(the regex you did) – kold-kreator Mar 19 '18 at 12:22
  • @KauêPorte glad to help , read first this https://httpd.apache.org/docs/2.4/rewrite/intro.html then this http://httpd.apache.org/docs/current/mod/modse_tenvif.html so , i used mod_setenvif against URI then utilize from that by adding fixed parameters , your given parameters, then match against them in condition ,RewriteCond, according to their content and finally do the last rule according to previous condition – Mohammed Elhag Mar 19 '18 at 12:42
0

You can make the 3rd and 4th capture-groups optional in your pattern.

RewriteRule ^editar/([^/]*)/([^/]*)/?([^/]*)/?([^/]*)/?$ /edit.php?mode=$1&cat=$2subcat=$3&prod=$4 [NC,L]

But doing this, you will get empty subcat and product value if the uri is /editar/foo/bar .

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • That would be no problem. If the mode is CAT, only the first ID is necessary. The others won't be used. The thing is, in you code, only the editar/cat/1 works... editar/subcat/1/1 and editar/prod/1/1/1 are both not working. – kold-kreator Mar 17 '18 at 18:09