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 "", but now they are "".

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 "" and I click on that kind of link, I will get redirected to "" 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,


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;