如何使用 .htaccess 重定向图像的 url?
How to redirect urls for images with .htaccess?
旧网址:
1. https://example.com/images/1/image1.png
2. https://example.com/images/2/image2.png
3. https://example.com/images/3/image9.jpg
4. https://example.com/images/4/image19.jpg
新网址:
1. https://example.com/images/101.png
2. https://example.com/images/202.png
3. https://example.com/images/309.jpg
4. https://example.com/images/419.jpg
规则:
- 文件夹“1”+“0”+“1.png “
- 文件夹“2”+“0”+“2.png “
- 文件夹“3”+“0”+“9.jpg “
- 文件夹“4”+“19.jpg”
现在我有了 .htaccess 的代码:
RewriteEngine On
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/ [R=301,L]
# Rewrite index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [R=301,L]
# Rewrite Query/ to Query
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ / [R=301,L]
RewriteRule ^24$ / [R=301,L]
# Rewrite Query to index.php?route=Query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?route= [QSA,L]
在 RewriteEngine On
行之后立即尝试以下操作:
RewriteRule ^images/(\d+)/image(\d)\.(png|jpg)$ /images/. [R=301,L]
RewriteRule ^images/(\d+)/image(\d{2,})\.(png|jpg)$ /images/. [R=301,L]
第一条规则处理包含单个数字的图像名称(必须以 0
为前缀),第二条规则处理其余部分。
</code>、<code>
和 </code> 反向引用匹配前面 <code>RewriteRule
模式 中相应的捕获子组。换句话说,</code> 包含文件夹名称(所有数字),<code>
包含文件名后出现的数字,</code> 包含文件扩展名(<code>jpg
或 png
).
注意:首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。
澄清一下,大概您已经在内部链接到新的图像 URL?
旧网址:
1. https://example.com/images/1/image1.png
2. https://example.com/images/2/image2.png
3. https://example.com/images/3/image9.jpg
4. https://example.com/images/4/image19.jpg
新网址:
1. https://example.com/images/101.png
2. https://example.com/images/202.png
3. https://example.com/images/309.jpg
4. https://example.com/images/419.jpg
规则:
- 文件夹“1”+“0”+“1.png “
- 文件夹“2”+“0”+“2.png “
- 文件夹“3”+“0”+“9.jpg “
- 文件夹“4”+“19.jpg”
现在我有了 .htaccess 的代码:
RewriteEngine On
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/ [R=301,L]
# Rewrite index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [R=301,L]
# Rewrite Query/ to Query
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ / [R=301,L]
RewriteRule ^24$ / [R=301,L]
# Rewrite Query to index.php?route=Query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?route= [QSA,L]
在 RewriteEngine On
行之后立即尝试以下操作:
RewriteRule ^images/(\d+)/image(\d)\.(png|jpg)$ /images/. [R=301,L]
RewriteRule ^images/(\d+)/image(\d{2,})\.(png|jpg)$ /images/. [R=301,L]
第一条规则处理包含单个数字的图像名称(必须以 0
为前缀),第二条规则处理其余部分。
</code>、<code>
和 </code> 反向引用匹配前面 <code>RewriteRule
模式 中相应的捕获子组。换句话说,</code> 包含文件夹名称(所有数字),<code>
包含文件名后出现的数字,</code> 包含文件扩展名(<code>jpg
或 png
).
注意:首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。
澄清一下,大概您已经在内部链接到新的图像 URL?