简体   繁体   中英

Clean url in php with two get parameters

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM