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.

2
  • 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 Commented Feb 4, 2016 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.) Commented Feb 5, 2016 at 0:00

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Also worth noting that the Last flag has been removed from the non-last RewriteRule, sounds obvious, but people miss those sorta details...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.