regex - .htaccess 将具有特定路径的 URL 重定向到 404 并排除一些具有相同路径的 URL 作为异常

标签 regex apache .htaccess mod-rewrite regex-lookarounds

我有一个关于 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/

相关文章:

.htaccess - htaccess 文件将根页面以外的所有页面重定向到其他域(好主意与否?)

java - REGEX:如何使该语句成为 REGEX 中的 2 个匹配项

regex - Nginx - 在 uwsgi_pass 之前重写 request_uri

python - Pandas 系列 str.extract 无法匹配正则表达式模式

linux - 清除大型 Apache 域日志

ruby - 如何将 apache 日志时间转换为 activerecord :datetime

.htaccess 添加额外的 'C:/xampp/htdocs/' 来重定向 xampp/localhost 上的 url - 如何删除?

java - Java 中用于组匹配的正则表达式

php - 将 php session 移动到 redis。有可能不丢失现有 session 数据吗?

.htaccess -::$ 1在htaccess中是什么意思?