.htaccess重定向一个干净的图像网址

时间:2013-11-05 18:31:05

标签: apache .htaccess mod-rewrite url-rewriting clean-urls

我正在尝试为图像执行干净的URL,其中url还包含图像的描述(用于seo目的)。文件名唯一重要的部分是开头和扩展名。

我想尝试使用图片网址,例如:

  

mywebsite.com/seo-images/的 123 - 一些-description.jpg

在图像实际位于

的位置重写一个干净的网址
  

mywebsite.com/uploads/的 123 .JPG

文件类型可以是.jpg或.png或.gif等。

我也有缩略图:

  

mywebsite.com/seo-images/的 T123 - 一些-description.jpg

也要以相同的方式和地点做一个干净的网址重写

  

mywebsite.com/uploads/的 T123 .JPG

我尝试将以下.htaccess文件放在seo-images目录中

RewriteEngine On
RewriteBase /seo-images/

# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([_0-9a-zA-Z-]+/)?-([_0-9a-zA-Z-]+/)?([_0-9a-zA-Z-]+/)\.$ /uploads/$1.$3 [R=301,L]
到目前为止,没有好处。

我在root中有一个.htaccess文件,用于执行其他清理mod_rewrites。

“some-description”可以是一个单词或多个单词分隔 - 所以基本上是 - 之后的所有内容。应该被忽略,新的文件名用于在不同的目录中使用新文件名重写

1 个答案:

答案 0 :(得分:1)

我认为你会使用/seo-images/.htaccess中设置的这个更简单的规则,我们只在%{REQUEST_FILENAME}既不是文件也不是目录时重写,而不是反过来:

RewriteEngine On
#RewriteBase /seo-images/

# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-.*\.([[:alnum:]]+)$ /uploads/$1.$2 [L]

如果没有重定向,我们只会使用内部重写,Google会一直考虑mywebsite.com/seo-images/123-some-description.jpg而不是mywebsite.com/uploads/123.jpg。

或者您更喜欢绝对重写:

RewriteEngine On
#RewriteBase /seo-images/

# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-.*\.([[:alnum:]]+)$ /uploads/$1.$2 [R=301,L]

这样做:

/seo-images/123-some-description.jpg => /uploads/123.jpg
/seo-images/t123-foo-bar.png => /uploads/t123.png