首页 > 解决方案 > .htaccess 中的 301 重定向错误?WP网站

问题描述

website.com在自己的子域上重新制作了 Wordpress 上的旧网站remake.mydomain.com,现在我想将其移至原始域website.com,但我的大部分 URL 都已更改,因此我想重定向到新网站,但其中一些不要按预期重定向(至少对我来说,因为我是 .htaccess 文件的菜鸟):

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Redirect 301 /en /
Redirect 301 /en/ /
# this works

...

Redirect 301 /en/wheel-alignment-2 /service/wheel-alignment/
Redirect 301 /en/wheel-alignment-2/ /service/wheel-alignment/
# redirects to /wheel-alignment-2 (page not found)

Redirect 301 /en/tire-mounting-2 /service/tire-mounting/
Redirect 301 /en/tire-mounting-2/ /service/tire-mounting/
# redirects to /tire-mounting-2 (page not found)

Redirect 301 /en/news--sales /news-and-sales/
Redirect 301 /en/news--sales/ /news-and-sales/
# redirects to /news--sales (page not found)

...

Redirect 301 /en/testimonials /
Redirect 301 /en/testimonials/ /
# this is the last redirect, redirects to /testimonials (page not found)

甚至重定向到 /wp-content/ 中的图像,但我发现 Yoast SEO 导致了这种情况,因此在“搜索外观”>“媒体”中关闭了“媒体和附件 URL”,它工作正常。

顺便说一句,如您所见,旧网站在 URL 中有语言扩展名,我不再需要它了。

最终结果应该是这样的:

website.com/en > website.com
website.com/en/wheel-alignment-2 > website.com/service/wheel-alignment
website.com/en/news--sales > website.com/news-and-sales

如果我做错了,是否有人对出了什么问题以及如何正确执行此操作有任何建议?

正如我之前所说,我是 .htaccess 的菜鸟,可能应该使用重定向条件和规则,但尽管我在谷歌上搜索,但我无法掌握这些概念,也没有找到任何简单的解释来说明这一切作品。

标签: wordpressapache.htaccessredirectmod-rewrite

解决方案


如果我理解正确,您想从旧域重定向到 website.com,如果是这种情况,则将以下内容添加到您的 .htaccess 文件的最顶部并保持其余规则不变。

请确保在测试 URL 之前清除浏览器缓存。

RewriteEngine ON
##Redirect from remake.mydomain.com TO website.com here.
RewriteCond %{HTTP_HOST} remake\.mydomain\.com [NC]
RewriteRule ^(.*)$ http://website.com%{REQUEST_URI} [R=301,NE,L]

##Redirecting from website.com/en TO website.com
RewriteRule ^en/?$ / [NC,R=301,L]

##Redirecting from website.com/en/wheel-alignment-2 TO website.com/service/wheel-alignment
RewriteRule ^en/wheel-alignment-2/?$ service/wheel-alignment [NC,R=301,L]

##Redirecting from website.com/en/news--sales TO website.com/news-and-sales
RewriteRule ^en/news--sales/?$ news-and-sales [NC,R=301,L]

推荐阅读