【问题标题】:Replace space, %20 and + with hyphens [duplicate]用连字符替换空格、%20 和 + [重复]
【发布时间】:2012-09-29 23:17:42
【问题描述】:

可能重复:
301 Redirect With Spaces

现在,我将每个 Image 命中重定向到包含该图像的 html 页面。

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} (bing.com|google|yahoo|stumbleupon.com|reddit.com|pinterest.com) [NC]
RewriteRule (.*)\.(gif|jpg|png)$ /$1.html [R,L]

由于图像通常包含空格,但 html 页面总是使用连字符,我需要一个解决方案来用连字符替换所有空格、%20 和 + 符号

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    301 Redirect to replace all spaces to hyphens

    解决问题

    RewriteEngine On
    RewriteCond %{THE_REQUEST} (\s|%20)
    RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
    RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]
    

    【讨论】:

    • 想接受,说要等2天;)
    • 第 4 行(最后一行)有错误。我不得不将 /$1-$2 更改为 $1-$2
    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 2013-09-26
    • 2020-12-15
    相关资源
    最近更新 更多