【问题标题】:Wrong 301 redirects in .htaccess? WP site.htaccess 中的 301 重定向错误? WP网站
【发布时间】:2021-04-07 16:15:03
【问题描述】:

我已经在我自己的子域 remake.mydomain.com 上的 Wordpress 上重新制作了一个旧网站 website.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 的菜鸟,可能应该使用重定向条件和规则,但尽管我用谷歌搜索,但我无法掌握这些概念,也没有找到任何简单的解释这一切是如何运作的。

【问题讨论】:

  • 欢迎来到 SO 并感谢您在问题中分享您的 htaccess 文件。您能否也请提及您要重定向/重写的从哪个网址到哪个网址?为了更好地理解您的问题,请更新您的问题。
  • 您好,谢谢您,网址不会改变,最终结果会保持不变,我只是在自己的域上创建了一个子域来创建新站点,然后我将迁移到原本的。我已经更新了我的问题,希望现在更清楚了。

标签: wordpress apache .htaccess redirect mod-rewrite


【解决方案1】:

如果我理解正确,您想从旧域重定向到 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]

【讨论】:

  • 我怀疑是否应该提及我的开发子域,因为在我移动它之后该站点将保持website.comRewriteCond %{HTTP_HOST} website\.com [NC] 会工作吗?谢谢。
  • @Adam,但你提到了Wordpress on my own subdomain remake.mydomain.com and now I would like to move it to the original domain website.com,你能不能更详细地提到你想要重定向的链接。
  • 对不起,我希望重定向是这样的: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 等等。我这样做是出于 SEO 的目的。
  • @Adam,所以您的重定向不起作用或出现任何错误?
  • 就像我在我的问题中写的那样,它以错误的方式重定向它,当我尝试你的答案时,它写了ERR_TOO_MANY_REDIRECTS
猜你喜欢
  • 2012-12-17
  • 2017-09-21
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 2012-05-15
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多