htaccess url重写多个url

时间:2015-05-13 13:53:13

标签: regex apache .htaccess mod-rewrite url-rewriting

我在使用.htaccess

在我的网站中重写网址时遇到问题

的index.php

$op = $_GET['op'];

switch ($op) {

     case "src":
        include("src.php");
     break;

     case "dts":
         include ("dts.php");
     break;

     default:
        include("home.php");
     break;
}

链接到重写

index.php?op=src
index.php?op=dts&idric=20&sp=2

的.htaccess

Options +FollowSymLinks
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)/$ index.php?op=$1 [L,QSA]
RewriteRule ^(.*)/idric/(.*)/sp/(.*)/$ index.php?op=$1&idric=$2&sp=$3 [L,QSA]

如果我写第一个链接www.mysite.com/src/它会显示正确的页面(src.php), 但如果我写第二个网址www.mysite.com/dts/idric/20/sp/2/,它会显示默认页面(home.php)。

1 个答案:

答案 0 :(得分:1)

RewriteCond仅适用于下一个RewriteRule。而你的最后一条规则被前一条规则所覆盖。

你的规则如下:

Options +FollowSymLinks
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/idric/([^/]+)/sp/([^/]+)/$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]

RewriteRule ^(.+?)/$ index.php?op=$1 [L,QSA]