tsoltysiak Posted May 31, 2011 Share Posted May 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237951-rewrite-is-working-but-changes-directory/ Share on other sites More sharing options...
wildteen88 Posted May 31, 2011 Share Posted May 31, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/237951-rewrite-is-working-but-changes-directory/#findComment-1223054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.