0

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
  • Do you have something like this in you code $op = $_GET['op']; ? Commented May 13, 2015 at 13:55

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately in this manner nothing works, not even the first link mysite.com/src/
With these rules on my Apache localhost/dts/idric/20/sp/2/ gave me $_GET['op'] as dts and ``localhost/src/` gave as src so something wrong on your side. Did you copy/paste as shown? Are there any other rules too?
actually my test url is this localhost/test/src/ and localhost/test/dts/idric/20/sp/2/
.htaccess is inside test dir
Sorry , I made a mistake and now it seems to be ok. If you would like to add a new intermediate link (mysite.com/dts/idric/20/ ) as I should do? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.