简体   繁体   English

htacess mod_rewrite正则表达式-包括数字但不包括字母

[英]htacess mod_rewrite regex - include numbers but exclude letters

I have always struggled with regex, and after reading up on it for 45 mins my head is spinning. 我一直在用正则表达式挣扎,在阅读了45分钟后,我的头开始旋转。 Negative lookaheads, what the...? 负面的前瞻,那是什么...? (?:/(?:(?!s\\d+).)*)+$<--- OMG!!! (?:/(?:( ?! s \\ d +)。)*)+ $ <--- OMG !!!

:( :(

So, I have a rule 所以我有一条规则

RewriteRule /([0-9]+) /?id=$1 [R]

and it works fine when the url is www.hi.com/123 网址为www.hi.com/123时效果很好

How can I make it refresh to / (the document root i nthis case) if the url is www.hi.com/123abc or www.hi.com/a123bc ? 如果网址为www.hi.com/123abcwww.hi.com/a123bc如何将其刷新为/ (在这种情况下为文档根目录)?

I just want to make sure only urls with numbers and nothing else are matched. 我只想确保只有带数字的网址和其他没有匹配的网址

I tried 我试过了

RewriteRule /([0-9]+)([^a-z]+) /map.htm?marker=$1 [R]

But that refreshes to www.hi.com/?id=404 , oddly enough. 但是奇怪的是,它刷新到了www.hi.com/?id=404

To match 1 or more numbers in regex it will be: 要在正则表达式中匹配1个或多个数字,它将是:

[0-9]+

To match 1 or more numbers or letters in regex it will be: 要匹配正则表达式中的1个或多个数字或字母,它将是:

[0-9a-zA-z]+

For your case RewriteRule rule will be: 对于您的情况,RewriteRule规则将是:

RewriteRule ^([0-9a-z]+)/?$ /map.htm?marker=$1 [NC,L,R=302]

which will match /123abc OR /123abc/ OR /123 OR /abc/ , note that trailing slash is optional. 将与/123abc OR /123abc/ OR /123 OR /abc/匹配,请注意,斜杠是可选的。 Flags I used are: 我使用的标志是:

NC    - Ignore Case
L     - Last
R=301 - Use http status 302 for resulting URL

I would strongly suggest you reading mod_rewrite Reference doc: Apache mod_rewrite Introduction 我强烈建议您阅读mod_rewrite参考文档: Apache mod_rewrite简介

However you also asked: 但是,您还问:

How can I make it refresh to / (the document root i nthis case) if the url is www.hi.com/123abc or www.hi.com/a123bc? 如果网址为www.hi.com/123abc或www.hi.com/a123bc,如何将其刷新为/(在这种情况下为文档根目录)?

That rule would be: 该规则将是:

RewriteRule ^([0-9a-z]+)/?$ / [NC,L,R=302]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM