【问题标题】:.htaccess Redirect issue: last parameter added automatically.htaccess 重定向问题:最后一个参数自动添加
【发布时间】:2017-01-21 21:02:53
【问题描述】:

我想使用 htaccess 文件重定向一些 URL。

我的 htaccess 文件如下:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect /old-post-type/old-parent-post/old-post-slug/ /new-post-type/new-post-slug/

***** Some More URLs *****

# END WordPress

它应该重定向到http://yourwebsitedomain.com/new-post-type/new-post-slug/,但是,它正在重定向到http://yourwebsitedomain.com/new-post-type/new-post-slug/old-post-slug/

“old-post-slug/”自动添加到最后我不知道为什么?

接受任何建议。

【问题讨论】:

  • 你好像在用WordPress,不如直接安装一下'Redirection plugin'?

标签: php wordpress .htaccess redirect


【解决方案1】:

我不建议像这样混合mod_alias 和mod_rewrite,RewriteRule 先运行,然后在 Redirect 之后再次运行,这会导致奇怪的事情发生。只需一直使用 mod_rewrite,在其他规则之前使用重定向并使用 L 标志,如下所示:

RewriteEngine On
RewriteBase /
#I put 302, use 301 if permanent redirect is what you are after.
RewriteRule ^old-post-type/old-parent-post/old-post-slug/$ /new-post-type/new-post-slug/ [L,R=302] 
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

只有当你真的必须使用 htaccess 进行重定向时,就像有人提到 wordpress 已经具有处理重定向的功能。

【讨论】:

  • "old-post-type/old-parent-post/old-post-slug" 这是示例,它将是帖子类型名称,我有许多具有不同帖子类型的 URL,因此对于每个URL 有 RewriteRule 吗?
  • 不,您不需要为每个 url 设置规则,您可以使用捕获组编写通用规则来重定向。您还没有真正提供足够的信息来说明您从什么重定向到什么,以便有人为您的站点提供工作规则。我建议阅读:例如stackoverflow.com/questions/20563772/…,以熟悉这些概念。解决类似问题的人的其他问题/答案的答案中有很多链接。
  • 我尝试使用旧站点的第一个 URL,并尝试使用新站点重定向:Redirect /featured-hotels/half-moon-bay-lodge-northern-california-coast/lodge-golf-links/ /featured-hotels/half-moon-bay-lodge/ 但它会重定向到 /featured-hotels/half-moon-bay-lodge/lodge- Golf-links/ /lodge-golf-links/ 自动添加到最后,我有太多这样的旧 URL,我想重定向所有的 SEO 目的。
【解决方案2】:

试试这个:

Redirect 301 /old-post-type/old-parent-post/old-post-slug/ http://your-domain.com/new-post-type/new-post-slug/

这应该可行。 301 代表“永久移动”。

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 2020-03-10
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多