You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需求:匹配含大写字母且非指定后缀文件的URL正则表达式(用于重定向)

Got it, let's work through this problem together. You’re looking for a regex to target URLs that have uppercase letters, but skip those pointing to static files like .jpg, .css, or .js—so you can redirect the qualifying ones to their lowercase versions. Here’s how to pull this off:

The Regex Breakdown

First, here’s the core regex that fits your requirements:

^https?://[^/?#]+/([^?#]*[A-Z][^?#]*)(?<!\.(jpg|css|js|png|gif))(?=[?#]|$)

Let’s break down each part to understand what it does:

  • ^https?://: Matches URLs starting with HTTP or HTTPS (adjust if you need to cover other protocols like FTP).
  • [^/?#]+: Captures the domain part, stopping at the first /, ?, or # (so we don’t mix up domain and path).
  • /: Matches the slash right after the domain.
  • ([^?#]*[A-Z][^?#]*): Captures the path segment, ensuring it contains at least one uppercase letter ([A-Z]) and doesn’t include query parameters or anchor links.
  • (?<!\.(jpg|css|js|png|gif)): A negative lookbehind that ensures the path doesn’t end with any of these static file extensions (you can add more like .svg or .pdf inside the parentheses if needed).
  • (?=[?#]|$): A positive lookahead that marks the end of the path—either where query parameters/anchors start, or the end of the entire URL.

Real-World Implementation Examples

Regex alone isn’t enough; you’ll need to pair it with your server’s rewrite rules to handle the redirect. Here are common setups:

Apache (mod_rewrite)

If you’re using Apache, add these rules to your .htaccess or server config:

RewriteEngine On

# Match URLs with uppercase letters in the path
RewriteCond %{REQUEST_URI} ^/([^?#]*[A-Z][^?#]*)
# Exclude static file extensions (case-insensitive)
RewriteCond %{REQUEST_URI} !\.(jpg|css|js|png|gif)$ [NC]
# Redirect to lowercase version with permanent 301
RewriteRule ^(.*)$ /${lc:$1} [R=301,L]

The [NC] flag makes the extension check case-insensitive (so .JPG or .Css are also excluded), and ${lc:$1} converts the captured path to lowercase.

Nginx

For Nginx, you can use either a regex rewrite or a more flexible Lua block. Here’s the regex approach:

server {
    # ... other server config ...

    rewrite ^(/[^?#]*[A-Z][^?#]*)(?<!\.(jpg|css|js|png|gif))(?=[?#]|$) $uri permanent;
}

Or if you prefer Lua for easier extension:

server {
    # ... other server config ...

    rewrite_by_lua_block {
        local uri = ngx.var.request_uri
        -- Check if URI has uppercase letters
        local has_uppercase = uri:match("[A-Z]")
        -- Check if it's a static file (add more extensions as needed)
        local is_static = uri:match("%.jpg$") or uri:match("%.css$") or uri:match("%.js$") or uri:match("%.png$")
        
        if has_uppercase and not is_static then
            -- Redirect to lowercase version with 301
            return ngx.redirect(uri:lower(), 301)
        end
    }
}

Quick Tips for Your Setup

  • Expand the excluded extensions: Just add more extensions to the regex’s parentheses (e.g., jpg|css|js|svg|pdf) to cover other static files you don’t want to redirect.
  • Test first: Before deploying, test the regex against your actual URLs to make sure it’s capturing exactly what you want (avoid false positives/negatives).
  • Choose the right redirect code: Use 301 (permanent) if this is a permanent change, or 302 (temporary) if you might roll back later.

内容的提问来源于stack exchange,提问作者Rishish Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:30:35