Regex .htaccess将具有特定路径的URL重定向到404,并排除具有与异常相同路径的某些URL

Regex .htaccess将具有特定路径的URL重定向到404,并排除具有与异常相同路径的某些URL,regex,apache,.htaccess,mod-rewrite,regex-lookarounds,Regex,Apache,.htaccess,Mod Rewrite,Regex Lookarounds,我在Codeigniter(3.1.11)上有一个应用程序。基本上,我想要一个404重定向的URL,这些URL在URI中有仪表板。例如: dashboard/foo/bar dashboard/foo-2/bar-2 dashboard/something ... 另外,我想在重定向规则中保留一些例外,因此,一些具有dashboard作为路径URI的特定URL应该从该重定向中排除。假设我想排除一些URL,如: dashboard/new-foo/new-bar dashboard/one-th

我在Codeigniter(3.1.11)上有一个应用程序。基本上,我想要一个404重定向的URL,这些URL在URI中有
仪表板
。例如:

dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...
另外,我想在重定向规则中保留一些例外,因此,一些具有
dashboard
作为路径URI的特定URL应该从该重定向中排除。假设我想排除一些URL,如:

dashboard/new-foo/new-bar
dashboard/one-thing/abc
dashboard/another-thing/xyz
我试了几次,但排除规则不起作用。它将所有URL重定向到404。这是我的.htaccess中的内容:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dashboard/new-foo/new-bar) [NC]  # Exclude this url (this condition is not working)
RewriteRule ^(.*)$ dashboard/$1 [R=404,L]

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]


我不是htaccess的专家,但我非常确定您需要两行重写,一行包含所有仪表板URL,另一行排除新的URL。每个RewriteCond都隐式地与“AND”连接,因此如果要排除许多不同的模式,并且需要第三个RewriteCond,则需要在第二个条件中使用“OR”连接第三个RewriteCond

e、 g

我还想提到另外两件事:1)您的重写规则重定向回a/dashboard URL,因此您可能会在此处继续查看。2) 您不需要打开重写引擎两次,在顶部打开一次就足够了


如果htaccess中的重写规则变得越来越复杂,那么您可以使用index.php文件来处理它(或Codeigniter中的其他方法)。

可以在一个规则中完成,并具有如下负面前瞻条件:

RewriteEngine on

RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

非常感谢。它很有魅力。唯一不起作用的是仅带有
/dashboard
的路径。如何包括
/dashboard
路径以同时重定向到404?谢谢!快到了!它将
/home/
重定向到404,但是
/home
仍然没有重定向。啊!对不起,我是说
仪表板
。请阅读
home
as
dashboard
My bad!括号后有一个点。移除了它,它创造了奇迹。非常感谢你的帮助!
RewriteEngine on

RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]