Jump to content

rewrite is working, but changes directory


tsoltysiak

Recommended Posts

Hi,

 

I'm trying to use mod_rewrite to make nice looking urls, it's working fine when there is only one variable to be passed, but I'm having trouble in the cases where there are 2 variables.  Here is the situation:

 

I want http://www.domain.com/var1.php to show the file http://www.domain.com/index.php?q=var1

 

I have that working well, and it's doing what it is supposed to do.  The thing is, for some situations, I need it to use special.php instead of index.php.  I figured that the best way would be to add /special/ to the url.  Sometimes there will be 1 variable and sometimes 2 variables, so in those instances I want it to do something like this:

 

http://www.domain.com/special/var1/ ------> http://www.domain.com/special.php?q1=var1

or

http://www.domain.com/special/var1/var2 ------> http://www.domain.com/special.php?q1=var1&q2=var2

 

The thing is, when I do this, it is displaying the proper file http://www.domain.com/special.php?q=var1, but all of the links that are written relative to the file link to the wrong directory, so are not working (A link that should go to http://www.domain.com/varX.php is showing up as http://www.domain.com/special/varX.php, or as http://www.domain.com/special/var1/varX.php if there were 2 variables)

 

 

Hopefully that makes sense.  I'm sure I'm not the only that has had this problem, but I had a hard time finding a solution on google.  Here is my .htaccess code that is almost working, except for that one hiccup. 

 

RewriteEngine on

RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^(.*)\.php$ index.php?q=$1 [QSA,L]
RewriteRule ^special/(.*)$ special.php?q=$1
RewriteRule ^special/(.*)/(.*)$ special.php?q1=$1&q2=$2 [QSA,L]

 

If anyone can help me through it, I would really appreciate it.  Thanks!

 

Tom

You need to rewrite your links so they start with a / eg

<a href="/varX.php">Link</a>

 

If you do not do  that the web browser will try to load varX.php within the /special directory (even though it doesn't actually exist).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.