url重写删除文件夹.htaccess


url rewrites removing folders .htaccess

我想从网站的URL中删除"articles"answers"artificial"文件夹,但当我试图将重写规则引入.htaccess文件时,我收到了错误。

http://www.example.com/articles/artificial/are_string_beans_all_right_on_the_candida_diet.php

尝试转换

http://www.example.com/are_string_beans_all_right_on_the_candida_diet.php

这是我的.htaccess文件的当前版本

DirectoryIndex index.php
AddDefaultCharset utf-8
RewriteCond %{HTTP_HOST} ^[^.]+'.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^index'.php/?$ / [L,R=301,NC]
RewriteRule ^((?!articles/artificial).*)$ articles/artificial/$1 [NC,L]

问题应该出现在最后一条规则上:你需要的是负面看待,即任何不符合articles/artificial的东西

正则表达式应该是:

((?<!(articles/artificial)).*)

但是这种可能性不起作用,因为.*是可变长度的(参见regex look arounds)

一个解决方案可能是用允许可变长度的正向前瞻取代反向前瞻。所以你的规则可能是:

RewriteRule ^(?!.*?articles/artificial.*?)(.*)$ /articles/artificial/$1