开发者

Help with a rewrite rule

I have the following RewriteRule:

RewriteRule ^/([^?.]+)$ /MyPage.aspx\?Name=$1 [NC,L]

Which works fine when my URL is: http://www.somedomain.com/Tony

I get the following result: /MyPage.aspx?Name=Tony but I need to change it so it works with this type of URL: http://www.somedomain.com/MemberPages/Tony

Can someone tell me what the Regexp should be, Unfortunately, I am not very good with Regular Expressions so any help 开发者_如何学编程is appreciated.

Thank you,

Tony


If you just want to optionally allow MemberPages/, you can do it like this:

RewriteRule ^/(MemberPages\/)?([^?.]+)$ /MyPage.aspx\?Name=$2 [NC,L]

? means optionally include the preceding element, even if that element is a full capture group like (MemberPages/). Then you have to change the back-reference to $2 to account for the additional capture group.

So this rule will work for both urls:

http://www.somedomain.com/Tony
http://www.somedomain.com/MemberPages/Tony

Will both go to:

/MyPage.aspx?Name=Tony


This will work for any second-level path:

RewriteRule ^/[^/]+/([^?.]+)$ /MyPage.aspx\?Name=$1 [NC,L]

This will work for /MemberPages/-prefixed paths

RewriteRule ^/MemberPages/([^?.]+)$ /MyPage.aspx\?Name=$1 [NC,L]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜