开发者

How to add an optional "param" in a regular expression in .htaccess

开发者 https://www.devze.com 2023-03-25 16:26 出处:网络
I\'ve following 2 rules in my .htaccess file - 1. RewriteRule ^myscript/([A-Za-z0-9]{0,1})\\?page=([0-9]?)$ /myscript.开发者_JAVA技巧php?action=check&var=$1&page=$2 [L]

I've following 2 rules in my .htaccess file -

1. RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.开发者_JAVA技巧php?action=check&var=$1&page=$2 [L]
2. RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [L]

so that visitng /myscript/d sends a request as /myscript.php?action=check&var=d I am trying to add an option page parameter so that visiting /myscript/d?page=5 sends the requests as /myscript.php?action=check&var=d&page=5

to achieve this I tried this

RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]

But this rules is being ignored and the request is sent as /myscript.php?action=check&var=d (i.e. 2nd rule from the above is being applied). What am I doing wrong here? What changes do I need to make it get it working?

Thanks for your help.


Use 2nd rule only and add QSA flag:

RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [QSA,L]

This will pass existing query parameter to the new URL:

/myscript/d?page=5&say=hello
=>
/myscript.php?action=check&var=d&page=5&say=hello

Apache documentation: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

0

精彩评论

暂无评论...
验证码 换一张
取 消