当前位置: 动力学知识库 > 问答 > 编程问答 >

.htaccess - 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/

分享给朋友:
您可能感兴趣的文章:
随机阅读: