Problem with mod_rewrite and links

Tag: mod-rewrite Author: onyx0118 Date: 2010-10-29

I'm developing a MVC-based application and I've now set the .htaccess rewrite rules. However, I'm having problems to make everything appear correctly. This is my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?a=$1 [L,NS]

I'm having problems with the links as at first the URLs were like "domain.com/folder/index.php?a=whatever/whatever2/asd", but now they are "domain.com/folder/whatever/whatever2/asd".

I was generating the links just like "<a href='index.php?a=new/qwerty'>Qwerty</a>", but now when I try to generate the links I must do something like "<a href='new/qwerty'>Qwerty</a>", and guess what? If I'm, for example, on "domain.com/folder/new/lol" and I click on that kind of link, I will get redirected to "domain.com/folder/new/new/qwerty" and it isn't meant to work like that.

I can't use absolute paths as the software is meant to be easy-to-install for users who download it, and I can't know where the users will install this software, nor I want users to have to modify anything of the code.

Thanks in advanced,

Ivan

Other Answer1

I finally solved it.

I made my own link generator function and I made it to delete the index.php part from $_SERVER['SCRIPT_NAME'] (which gives us the absolute path to the script). In case you had more than one script, just delete every possible script from that special variable.

function url($rel_path) {
  $start = str_replace("index.php", "", $_SERVER['SCRIPT_NAME']);
  return $start.$rel_path;
}