开发者

How to capture a 'sub-section' of a URL in a rewrite rule?

开发者 https://www.devze.com 2022-12-29 16:33 出处:网络
I know the title is a little bit strange, but here is what the URLs look like: /user/xxx/page /user/xxx/page?error=yyy

I know the title is a little bit strange, but here is what the URLs look like:

/user/xxx/page
/user/xxx/page?error=yyy

The rule for the first URL looks something like this:

RewriteRule ^user/(\d+)/page$    something.pl?id=$1 [L开发者_开发技巧]

And to make it work with the second URL, it becomes:

RewriteRule ^user/(\d+)/page(error=\d+)?$    something.pl?id=$1 [L]

My question is... how do I capture the error number? I tried both of these:

RewriteRule ^user/(\d+)/page(error=(\d+))?$    something.pl?id=$1&error=$2 [L]
RewriteRule ^user/(\d+)/page(error=(\d+))?$    something.pl?id=$1&error=$3 [L]

But it isn't working...

How can I do this?


You can simply use the QSA flag to get the original query appended to the new one:

RewriteRule ^user/(\d+)/page$    something.pl?id=$1 [L,QSA]

Now a request of /user/xxx/page?error=yyy will get rewritten to /something.pl?id=xxx&error=yyy.

0

精彩评论

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