URL路由 - 地址重写说明

地址重写规则:

.htaccess

Options 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#control + action
RewriteRule ^(\w+)(?:[/\-_\|\.,])(\w+)(?:[/\-_\|\.,])(.*)$ index.php\?c=$1-$2&rewrite=$3 [L]

#control 
RewriteRule ^(\w+)(?:[/\-_\|\.,])(.+)$ index.php\?c=$1&rewrite=$2 [L]

apache httpd.ini

[ISAPI_Rewrite]
RepeatLimit 32
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#control + action
RewriteRule ^(\w+)(?:[/\-_\|\.,])(\w+)(?:[/\-_\|\.,])(.*)$ index.php\?c=$1-$2&rewrite=$3 [L]

#control 
RewriteRule ^(\w+)(?:[/\-_\|\.,])(.+)$ index.php\?c=$1&rewrite=$2 [L]

nginx.conf

#control + action
rewrite ^(\w+)(?:[/\-_\|\.,])(\w+)(?:[/\-_\|\.,])(.*)$ index.php?c=$1-$2&rewrite=$3 last;
#control 
rewrite ^(\w+)(?:[/\-_\|\.,])(.+)$ index.php?c=$1&rewrite=$2 last;

iis 7

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <rewrite>
       <rules>
            <rule name="mzphp_1" stopProcessing="true">
                <match url="(\w+)(?:[/\-_\|\.,])(\w+)(?:[/\-_\|\.,])(.*)$" />
                <action type="Rewrite" url="index.php?c={R:1}-{R:2}&rewrite={R:3}" appendQueryString="true" />
            </rule>
            <rule name="mzphp_2" stopProcessing="true">
                <match url="(\w+)(?:[/\-_\|\.,])(.+)$" />
                <action type="Rewrite" url="index.php?c={R:1}&rewrite={R:2}" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
    </system.webServer>
</configuration>

mzphp 自定义 URL

1.非地址重写链接:

index.php?c=control-action&var=...

2.系统地址重写(该地址中的 / 分隔符和 [.html] 均可在conf文件中配置):

/control/action/param1/param1value/param2/param2value...[.html]

conf/conf.[env].php 配置文件:

    //url rewrite params
    'rewrite_info' => array(
        'comma' => '/', // options: / \ - _  | . , 
        'ext' => '.html',// for example : .htm
    ),

使用系统地址重写时,您可以使用url方法,来生成url:

function url($control, $action, $params = array()) ;

例:

echo url('index', 'index', array('id'=>1));
echo url('index-index', array('id'=>1));
echo url('index-index', 'id=1&time=2015');

3.自定义地址重写:

index.php?c=control-action&rewrite=param1/param1value...

例如,我们需要使用:/help/123/ 来映射 index.php?c=article-help&id=123

可以在 urlrewrite 重写文件中配置(以 .htaccess 语法为例):

RewriteRule ^(help)/(\d+)/?$ index.php\?c=article-$1&rewrite=id/$2 [L]

注:本例中 rewrite 参数传入的分割符(/),视 rewrite_info 中的 comma 而定。

results matching ""

    No results matching ""