使用 .htaccess 从 url 中剪切一个文件夹,该文件夹可以在 url 中的任何位置

Cutting a folder from url with .htaccess, said folder can be anywhere in the url

我已经彻底搜索了删除 url 中间文件夹的方法,如下例所示:

我有:

http://www.website.com/company/pages/section/content

我想删除 /pages/ 部分,使其显示如下:

http://www.website.com/company/section/content

值得注意的是,我已经使用了一些 .htaccess 来删除 .php 扩展名,所以这可能与所需的重定向冲突?这是我用来删除 .php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC]

如果您尝试删除在 url 之后充当根目录的文件夹,到目前为止我找到的解决方案似乎很有效,但是 /pages/ 文件夹实际上可以位于我工作的网站的 urls(有时它们在根目录下,有时在 /company/ 之后,有时在 /company/data/ 之后,等等)。唯一可以预测的是文件夹的名称,它始终是“/pages”。

仅供参考,这是我试过但无效的方法:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^pages/(.*)$ / [L,NC,R]

提前感谢任何可以提供帮助的人。

在 root .htaccess 中尝试此代码:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteRule ^(company)/((?!pages/).+)$ /pages/.php [L,NC]

RewriteCond %{DOCUMENT_ROOT}/\.php -f [NC]
RewriteRule ^([^.]+)/?$ .php [L]

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteCond %{THE_REQUEST} ^GET\ /pages/
RewriteRule ^pages/(.*) /company/section/content/ [L,R=301]

如果这行不通,试试这个

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteCond %{THE_REQUEST} ^GET\ /pages/company/section/content/
RewriteRule ^pages/company/section/content/(.*) /company/section/content/ [L,R=301]