【问题标题】:Use .htaccess to remove directory folder from URL使用 .htaccess 从 URL 中删除目录文件夹
【发布时间】:2015-12-17 16:41:19
【问题描述】:

此类问题之前已在 SO 中提出,但提供的解决方案对我不起作用。 How can I remove this directory/folder from url with htaccess rewrite?

我有一个我继承的多站点 wordpress 安装,它在站点的根目录中有一个 .htaccess 文件。

我需要对像 http:thewebsite.com/jol/blog/author/myles/ 这样的所有 URL 进行全局重定向以转到 http:thewebsite.com/jol/author/myles/

到目前为止,我已经在 .htaccess 文件中尝试过这段代码:

# BEGIN WordPress
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# Remove subdirectory of "blog" from URLs
RewriteCond %{THE_REQUEST} \ /blog/
RewriteRule ^blog/(.*)$ /$1 [L,R=301]

还有这段代码:

# BEGIN WordPress
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{THE_REQUEST} \ /blog/
RewriteRule ^blog/(.*)$ /$1 [L,R=301]

最后,这段代码:

# BEGIN WordPress
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^blog/(.*)$ /$1 [R=301,NC,L]

而且它们都完全不起作用。到目前为止,唯一成功的是单独定位每个 URL 并执行重定向,这是...痛苦的。

任何帮助将不胜感激。

【问题讨论】:

  • 模式^blog/ 会失败,因为您的网址以jol/ 开头
  • 谢谢!当我现在看到它时,这很有意义。那么我将如何定位“博客”的下一个子目录?
  • 试试这个RewriteRule ^(.*(?=blog/))blog/(.*)$ $1$2 [NC,L,R=301]
  • 你先生……是我的英雄!

标签: wordpress apache .htaccess mod-rewrite


【解决方案1】:

您尝试的规则不起作用,因为您的模式中有:^blog/。由于您要重写的 URL 的格式为 jol/blog/...,因此应该更新规则。

假设jol 可能会有所不同;使用规则:

RewriteRule ^(.+/(?=blog/))blog/(.*)$ $1$2 [R=301,L,NC]

如果jol 总是在规则中,就使用它:

RewriteRule ^jol/blog/(.*)$ jol/$1 [R=301,L,NC]

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 2015-07-27
    • 1970-01-01
    • 2022-09-27
    • 2013-10-23
    • 2013-05-28
    • 2018-04-22
    相关资源
    最近更新 更多