除了特定模式之外的所有内容的重写规则

时间:2013-05-17 03:27:20

标签: 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>

粗略地说: 如果请求未指向文件或目录或符号链接,那么它不是/forum 如果没有使用论坛启动,请指向/index.php/url。

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

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

3 个答案:

答案 0 :(得分:1)

RewriteRule !^forum/ index.php/something_else_that_is_not_forum/blah_blah_blah

任何不以......开头的事情。

答案 1 :(得分: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>

答案 2 :(得分:0)

您也可以尝试:

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