2

I am working on a PHP script that uses clean URIs.

My problem is, that I have one page that first uses no get parameter, then one and at the end two.

The line in the .htaccess file currently looks like this:

 RewriteRule ^birthing-records/([^/]+)/?$ birthing-records.php?url=$1 [L,QSA,NC]

But if I add the second parameter like this:

RewriteRule ^birthing-records/([^/]+)/([^/]+)/?$ birthing-records.php?url=$1&second=$2 [L,QSA,NC]

The script redirects me to the error page.

How do I have to set this up?

Do I need two lines in the .htaccess for that case?

I would normally solve this by simply calling another page but I would like to keep the exact URIs I am using right now because all of those pages are indexed at Google. I would really appreciate any help.

Martin
  • 22,212
  • 11
  • 70
  • 132
Dennis
  • 595
  • 1
  • 6
  • 22
  • I think you should not use Google as an excuse for not changing URIs or for how you act upon your website, Google will re-index your page within a few days or a few weeks (depending on traffic flow) and Google will benefit from re-indexing if it makes use of more content descriptive URIs – Martin Feb 04 '16 at 23:56
  • _“Do I need two lines in the .htaccess for that case?”_ – of course you do, if you want to catch stuff such as `birthing-records/123`, because your modified rule now _requires_ that a second slash-something combination follows. (Or you need to make that second occurrence optional as well.) – CBroe Feb 05 '16 at 00:00

1 Answers1

2

You need one rule for each, otherwise whenever you put one param it'll break.

RewriteRule ^birthing-records/([^/]+)/?$ birthing-records.php?url=$1 [QSA,NC]
RewriteRule ^birthing-records/([^/]+)/([^/]+)/?$ birthing-records.php?url=$1&second=$2 [L,QSA,NC]

Thanks to @Martin for this note: that the L option of the first one has been removed since the L option indicates the last rule to be run (only one rule can exist with the L option)

Otherwise, as far as I can tell, they each work fine individually, but if you want to accept 1 OR 2 parameters, then two rules is what you need.

You can test htaccess stuff here: http://htaccess.mwl.be/

Architect Nate
  • 674
  • 1
  • 6
  • 21
  • 1
    Also worth noting that the `L`ast flag has been removed from the non-last RewriteRule, sounds obvious, but people miss those sorta details... – Martin Feb 05 '16 at 00:04