htaccess url rewrite multiple url

筅森魡賤 提交于 2019-12-14 03:13:50

问题


I've a problem to rewrite url in my site with .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;
}

Link to rewrite

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]

If I write the first link www.mysite.com/src/ it shows the correct page (src.php), but if I write the second url www.mysite.com/dts/idric/20/sp/2/ it shows the default page (home.php).


回答1:


RewriteCond is only applier to very next RewriteRule. And your last rule gets overridden by previous one.

Have your rules like this:

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]


来源:https://stackoverflow.com/questions/30216960/htaccess-url-rewrite-multiple-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!