.htaccess - How to do a mod_rewrite with varying number of variables -


this question has answer here:

i have mod_rewrite rule has 3 variables so:

rewriterule ^page/(.*)/(.*)/(.*)$ page.php?a=$1&b=$2&c=$3 

the 3rd variable isn't mandatory, first 2. problem occurs when attempts hit url first 2 in place page/blah/blah or page/blah/blah/

i understand can have 2 rules, e.g:

rewriterule ^page/(.*)/(.*)/(.*)$ page.php?a=$1&b=$2&c=$3 rewriterule ^page/(.*)/(.*)$ page.php?a=$1&b=$2 

but seems longwinded.

how can write flexible rule allows 3 url possibilities arrive @ same page?

  1. /page/blah/blah/blah/
  2. /page/blah/blah
  3. /page/blah/blah/

you may try in 1 .htaccess file @ root directory:

options +followsymlinks -multiviews rewriteengine on rewritebase / rewritecond %{request_uri} !page\.php  [nc] rewriterule ^page/([^/]+)/([^/]+)/?([^/]+)?/?  /page.php?a=$1&b=$2&c=$3  [l,nc] 

third parameter optional , key c empty when there 2 parameters.

if don't want empty key value, replace above rule-set one:

options +followsymlinks -multiviews rewriteengine on rewritebase / rewritecond %{request_uri} !page\.php  [nc] rewriterule ^page/([^/]+)/([^/]+)/([^/]+)/?  /page.php?a=$1&b=$2&c=$3  [l,nc] rewritecond %{request_uri} !page\.php  [nc] rewriterule ^page/([^/]+)/([^/]+/?$  /page.php?a=$1&b=$2  [l,nc] 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -