Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

apache - Match Question Mark in mod_rewrite rule regex

I am looking to rewrite urls with multiple substrings. One substring is being requested as a subdirectory, while any others are requested as normal query string parameters.

For example, I would like to rewrite urls from

http://www.mysite.com/mark/friends?page=2

to

http://www.mysite.com/friends.php?user=mark&page=2

I am able to accomplish this with the exception of the question mark character. Here is my rewrite rule:

...
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-_]+)/friends[?]?([^/.]+)?$ friends.php?user=$1&$2 [L]

If I change the question mark to any other character it works great. It seems like the problem is that the '?' character is being interpreted incorrectly as the start of a new query string.

I need to pass on any parameters that appear after /user/friends as is. How do I accomplish this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should be using the [QSA] flag instead of trying to rewrite the query string. [QSA] passes on the query string to the rewritten URL.

So your rule should look like:

...
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-_]+)/friends/? friends.php?user=$1 [QSA,L]

Your case is very similar to the example given for using the QSA flag in the mod_rewrite cookbook.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...