0

I have the following rewrite.

RewriteRule ^/news/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /pagebase.php?pbid=3656&nid=$1&title=$2 [QSA,L,I]

http://www.domain.com/news/1/new-event/

So that the above url will be rewritten as:

http://www.domain.com/pagebase.php?pbid=3656&nid=1&title=new-event

This works perfectly. However I want people to be able to type in:

http://www.domain.com/news without any query strings and have it rewrite as this:

http://www.domain.com/pagebase.php?pbid=3656&nid=&title=

However the match fails so I get a 404 error. Is there anyway I can rewrite my rule to make the last 2 query string options optional. I was able to figure it out using multiple rewrite rules and placing them in the correct order, but I'd like to get it so I can get it working with one rule.

TroySteven
  • 4,885
  • 4
  • 32
  • 50
  • I'm guessing on this, but you can experiment with something along the lines of this: `RewriteRule ^/news(/?|([a-z0-9\-]+)/([a-z0-9\-]+)/?)$ /pagebase.php?pbid=3656&nid=$2&title=$3 [QSA,L,I]` What I did was to break the parts after "news" into two options - either an optional slash and nothing else or the rest of the structure. I believe the overall parenthetical expression would be $1 while the two enclosed original expressions would be $2 and $3. – David Ravetti Feb 28 '13 at 23:10

1 Answers1

2

Use two rewrite rules:

RewriteRule ^/news/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /pagebase.php?pbid=3656&nid=$1&title=$2 [QSA,L,I]
RewriteRule ^/news$ /pagebase.php?pbid=3656&nid=&title= [QSA,L,I]
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109