我有一个关于 Codeigniter(3.1.11) 的应用程序。基本上,我想要一个具有 dashboard
的 URL 的 404 重定向。在 URI 中。像这些:
dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...
另外,我想在重定向规则中保留一些异常(exception),因此,某些具有 dashboard
的特定 URL因为路径 URI 应该从这个重定向中排除。假设我想排除一些 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]
最佳答案
它可以在单个规则中完成,具有如下负面前瞻条件:
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]
关于regex - .htaccess 将具有特定路径的 URL 重定向到 404 并排除一些具有相同路径的 URL 作为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64458232/