regex - 重写除特定模式以外的所有内容的规则

标签 regex .htaccess

我希望 http://my_domain.com/forum/index.php/blah_blah_blah 不被重定向到任何地方。

但我希望将 http://my_domain.com/something_else_that_is_not_forum/blah_blah_blah 重定向到 http://my_domain.com/index.php/something_else_that_is_not_forum/blah_blah_blah

所以基本上,只有没有 http://my_domain.com/forum/ 前缀的所有内容都应该被重定向。

我有这个 .htaccess:

<IfModule mod_rewrite.c>
    # Options +FollowSymLinks -Indexes
    RewriteEngine On
    RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    RewriteRule ^(?!forum.*)(.*)$ index.php/$2 [L,QSA]
</IfModule>

我认为正则表达式应该完全符合我的要求,但在这种情况下,我似乎错了。 http://www.getnocms.com/forum/index.php?action=admin;area=manageboards;sa=newcat;df105e678e9b=e1a979a0631bd203b6794debc16ceced

有人知道如何正确地做到这一点吗?

编辑: 我用这个

<IfModule mod_rewrite.c>
    # Options +FollowSymLinks -Indexes
    RewriteEngine On
    RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI} !^\/forum 
    RewriteRule (?!^forum(?:/.*|)$)^(.*)$ /index.php/$1 [L,NC,QSA]
</IfModule>

粗略地说: 如果请求未指向文件或目录或符号链接(symbolic link),并且它不是 /forum 那么 如果不是以forum启动,则指向/index.php/url。

这似乎是合乎逻辑的(好吧,我们实际上不需要 RewriteCond %{REQUEST_URI} !^\/forum),但是访问这个 url 时仍然失败:http://www.getnocms.com/forum/index.php?action=admin;area=manageboards;sa=newcat;a5272d5=2fcf142818fc9df2aabb1364942a1d14

也许重写规则不适用于分号?我不确定。

最佳答案

您的规则几乎是正确的,但需要进行一些小的改动。用这个替换你的代码:

<IfModule mod_rewrite.c>
    # Options +FollowSymLinks -Indexes
    RewriteEngine On
    RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule (?!^forum(?:/.*|)$)^(.*)$ /index.php/$1 [L,NC]
</IfModule>

关于regex - 重写除特定模式以外的所有内容的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600917/

相关文章:

apache - .htaccess 使用子文件夹重写规则强制 SSL

.htaccess - 如何限制网站被索引

wordpress - 使用 .htaccess 将 WordPress 上传文件夹重定向到 s3 存储桶

.htaccess 图片源从http重定向到https

c++ - 匹配里面没有连字符的单词

java - 如何使用 DFA 正则表达式匹配器实现正则表达式断言/环视(即\b 样式词边界)

PHP Preg 匹配 - 获取包装值

javascript - 用逗号分隔的正则表达式重复模式

javascript - 在正则表达式中,我需要仅在中间允许空格并在开头和结尾处防止空格

.htaccess - 谷歌如何对重定向使用react