用连字符替换空格,%20和+

时间:2012-10-09 15:14:07

标签: apache .htaccess mod-rewrite

  

可能重复:
  301 Redirect With Spaces

现在我正在将每个图像点击重定向到包含图像的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和+符号

1 个答案:

答案 0 :(得分:5)

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]