【问题标题】:Construct URL Rewriting rule for htaccess for 404 Not Found为 404 Not Found 构建 htaccess 的 URL 重写规则
【发布时间】:2014-11-18 14:16:06
【问题描述】:

我需要 URL 重写方面的帮助,以便在找不到请求的文件时提供默认图像。

我想为 3 种不同的图像 URL 格式编写规则:

(1)http://example.com/imgs/user/123-profile.jpg

(2)http://example.com/imgs/user/123-profile-987.jpg

(3)http://example.com/imgs/user/123-678.jpg

如果未找到 (1) 和 (2),则应提供“http://example.com/imgs/user/default-user.jpg”;如果 (3) 未找到,则应提供“http://example.com/imgs/user/default-post.jpg”。

我在“.../imgs/user/.htaccess”文件中尝试了以下代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)imgs/user/([0-9]*)[-profile]([0-9]*).jpg$ /imgs/user/default-user.jpg [L]
RewriteRule ^(.*)imgs/user/([0-9]*)[-]([0-9]*).jpg$ /imgs/user/default-post.jpg [L]

还请告诉我 - 我应该将代码放在“/”(文档根目录)的 .htaccess 中还是更首选当前路径。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting http-status-code-404


    【解决方案1】:

    您的正则表达式似乎不正确,并且两条规则都需要RewriteCond

    你可以这样写代码:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^.*?imgs/user/\d+-profile[^.]*\.jpe?g$ /imgs/user/default-user.jpg [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^.*?imgs/user/\d+-\d+.jpe?g$ /imgs/user/default-post.jpg [L,NC]
    

    【讨论】:

    • 感谢您抽出宝贵时间,但遗憾的是它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多