在 htaccess 中使用正则表达式进行 301 重定向

问题描述 投票:0回答:1

有些东西正在访问我以 https:/** 而不是 https://** 开头的网站图片,所以我一直收到 404 错误。

如何在 htaccess 中使用正则表达式来重定向这些访问:

https:/example.com/image/cache/catalog/firstimage.jpg
https:/example.com/image/cache/catalog/someotherimage.jpg
https:/example.com/image/cache/catalog/animatedimg.gif

https://example.com/image/cache/catalog/firstimage.jpg
https://example.com/image/cache/catalog/someotherimage.jpg
https://example.com/image/cache/catalog/animatedimg.gif
.htaccess redirect expression
1个回答
0
投票

是的,您可以在“.htaccess”文件中使用正则表达式来设置 301 重定向。这是一个如何做的例子:

假设您想将所有对包含字符串“old-page”的 URL 的请求重定向到一个新的 URL:

RedirectMatch 301 /.*old-page.* https://example.com/new-page

此代码会将任何包含字符串“old-page”的 URL 重定向到 https://example.com/new-page。正则表达式中的 .* 匹配“old-page”前后的任何字符。

这里有一些其他的例子:

将特定的旧页面重定向到新页面:

Redirect 301 /old-page.html https://example.com/new-page.html

将目录重定向到新域:

RedirectMatch 301 ^/old-directory/(.*)$ https://new-domain.com/new-directory/$1

将目录中的所有页面重定向到新域的主页:

RedirectMatch 301 ^/old-directory/(.*)$ https://new-domain.com/
© www.soinside.com 2019 - 2024. All rights reserved.