【问题标题】:rewrite rule for Everything but certain pattern重写除特定模式之外的所有内容的规则
【发布时间】:2013-05-17 03:27:20
【问题描述】:

我希望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>

大致上说: 如果请求没有指向文件或目录或符号链接,并且它不是/forum 那么 如果不是以论坛开头,则指向/index.php/url。

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

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

【问题讨论】:

    标签: regex .htaccess


    【解决方案1】:

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

    <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>
    

    【讨论】:

    • 非常合理。你的规则似乎是合乎逻辑的。但是,访问这个网址:getnocms.com/forum/… 似乎也被重定向到 /index.php
    • 我实际上是在本地测试后发布的,我用你的 URL 重新测试了这些规则,它仍然工作正常,没有转到 /index.php 你确定这是你的 .htaccess 中唯一的代码吗并且在其他一些子目录中没有其他 .htaccess?
    • 我确定,我怀疑“;”有问题特点。这是工作getnocms.com/forum/…,而这不是:getnocms.com/forum/… 但是我会接受你的答案作为最完整的答案,并且可能会在问题缩小后开始一个新问题
    • 我提出了一个新问题:stackoverflow.com/questions/16631786/…,如果您有兴趣回答。谢谢:)
    • 感谢您的接受,我会尝试解决您的新问题。
    【解决方案2】:
    RewriteRule !^forum/ index.php/something_else_that_is_not_forum/blah_blah_blah
    

    任何不以...开头的东西

    【讨论】:

    • 我仍然需要 url,所以我想我应该这样做: RewriteRule !^forum/(.*)$ index.php/$1 [L,QSA] 但它仍然不起作用
    • 我有类似的东西适合我。尝试一些变体: RewriteRule !^/forum/;重写规则!^/论坛
    【解决方案3】:

    你也可以试试:

    RewriteCond %{REQUEST_URI} !^/forum 
    RewriteRule ^(.)$ http://domain.com/$1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-11
      • 2010-12-13
      相关资源
      最近更新 更多