【问题标题】:How to Redirect example.com/blog/month/post to example.com/post如何将 example.com/blog/month/post 重定向到 example.com/post
【发布时间】:2016-01-12 09:44:29
【问题描述】:

我最近创建了一个与 Serverpilot 一起使用的新 droplet。因此,我创建了一个新的 droplet,并使用 All in one WP 迁移插件从我的旧 droplet 迁移了内容。在永久链接设置中,我维护了以前的永久链接结构,即 example.com/post-name 并保存了设置。

但是,突然之间,我可以看到 example.com/blog/dd-mm-yy/post-name 的自然流量(搜索引擎流量),这显然是返回未找到的 404 页面。

我不知道这是否是临时问题,因为我已经设置了正确的永久链接结构。

现在我想将任何访问 example.com/blog/date/post-name 的流量重定向到 example.com/post 名称。对于所有的帖子。基本上,我想从所有帖子中删除 /blog/dd-mm-yy,以便可以将用户重定向到 example.com/post-name。

有什么方法可以从 .htaccess 中做到这一点?

.htaccess 文件如下所示:

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] RewriteRule ^blog/(?:\d+/)+/([^/]+/?)$ /blog/$1 [L,NC,R=301] 
RewriteBase / 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule>

【问题讨论】:

    标签: wordpress .htaccess redirect


    【解决方案1】:

    /.htaccess 内部,将这条规则作为第一条规则

    RewriteEngine On
    RewriteBase / 
    
    RewriteRule ^blog/(?:\d+/)+/([^/]+/?)$ /blog/$1 [L,NC,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. 
    RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L]
    
    RewriteRule ^index\.php$ - [L] 
    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . /index.php [L] 
    

    【讨论】:

    • 嗨 Anubhav,感谢您的回复。我已经编辑了这个问题。我想将 example.com/blog/dd-mm-yy/post-name 重定向到 example.com/post-name ... 那么,我应该添加以下规则吗? RewriteEngine On RewriteRule ^data/(.*)$ /blog/$1 [L,NC,R=301]
    • 嗨 Anubhav,帖子 URL 是这样的 >> example.com/blog/2016/01/06/postname 抱歉,问题中没有正确回答。所以,我添加了更新的规则,但我看到网站没有被重定向……这是因为日期格式错误吗?我需要再次对规则进行任何更改吗?
    • 抱歉回复晚了,但我无法让它工作。我最终使用重定向插件手动重定向了所有帖子。
    • 但是,我想补充一点,我从未为我的网站创建 /blog 子文件夹。它一定是由主题添加的,我猜只是一个类别。因此,您的建议无效。
    • 嗨 anubahv,请参阅: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\。 RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] RewriteRule ^blog/(?:\d+/)+/([^/]+/?)$ /blog/$1 [ L,NC,R=301] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule 。 /index.php [L] 应该是这个样子吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2011-02-28
    • 2018-01-09
    • 2018-07-05
    • 1970-01-01
    • 2019-05-18
    相关资源
    最近更新 更多