Home Articles

两个mod重写规则

Asked
Viewed 1087 times
2

我需要两个条件规则 . 背景:

http://www.domain.com/.htaccess
http://www.domain.com/index.php
http://www.domain.com/admin/index.php

我有两个重写规则:

ROOT:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/]+)/ $1.php?%1 [L]

ADMIN:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/([a-z]+?)/(.*)$ index.php?model=$1&view=$2&params=$3 [L,NS]

我需要以某种方式只有根级别上的一个.htaccess文件使用这两个规则,但ADMIN规则仅在url http://www.domain.com/admin/foo/bar/something/else 和ROOT规则适用于 http://www.domain.com/foo/bar/something/else 时应用 .

提前谢谢你,我已经杀了一整天试图这样做,当一个工作时,另一个给我一个“未找到”页错误 .

短剑的一种

2 Answers

  • 2

    我用这个:

    RewriteEngine On
    
    # /admin/ rule
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^admin/([a-z]+)/([a-z]+)/(.*)$ admin/index.php?model=$1&view=$2&params=$3 [L,NS]
    RewriteRule ^admin/ - [L]
    
    # other rules
    RewriteRule ^(.*/)([^/]+)/([^/]+)$ $1?$2=$3 [L,QSA]
    RewriteRule ^([^/]+)/ $1.php [L,QSA]
    

    QSA 标志(查询字符串追加)替换 RewriteCond %{QUERY_STRING} (.*) 指令 .

  • 1
    RewriteCond %{REQUEST_URI} !^/admin/
    RewriteCond %{QUERY_STRING} (.*)
    RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [L]
    RewriteCond %{REQUEST_URI} !^/admin/
    RewriteCond %{QUERY_STRING} (.*)
    RewriteRule ^([^/]+)/ $1.php?%1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^admin/([a-z]+?)/([a-z]+?)/(.*)$ /admin/index.php?model=$1&view=$2&params=$3 [L,NS]
    

Related