URL重写正则优化:如何仅匹配不含点号的纯文本字符串
Hey there! I get what you're after—ditching that suffix-specific regex for a universal one that rejects any string containing a dot (.), right? Let's get this sorted for your Tuckey URL Rewrite config.
The Regex You Need
Instead of excluding specific extensions, use this regex to match only strings with zero dots:
^[^.]+$
Breakdown of how this works:
^anchors the match to the start of the string[^.]+matches one or more characters that are NOT a dot (the[^...]syntax is a negated character class)$anchors the match to the end of the string
This ensures the entire string has no dots at all—no more worrying about missing random extensions!
Example Usage in Tuckey's XML Config
Here's how you'd drop this into your URL rewrite rules. Let's say you want to rewrite requests for dot-free paths to a target page, capturing the path as a parameter:
<rule> <!-- Match any URL path with no dots --> <from>^/([^.]+)$</from> <!-- Rewrite to your target, using the captured path as $1 --> <to>/content-handler?requested-path=$1</to> </rule>
The ([^.]+) captures the dot-free portion of the URL, which you can reference with $1 in your <to> directive.
Quick Note
If you need to allow empty strings (though that's rare for valid URLs), swap + for * to match zero or more non-dot characters:
^[^.]*$
内容的提问来源于stack exchange,提问作者Steven Krol

