Search code examples
phpapachemod-rewriteclean-urls

.htaccess mod_rewrite does not work with two parameters on localhost


I would like to have clean URLs in my projects. So I've written these codes in a .htaccess file.

RewriteEngine On  
Options +FollowSymLinks  

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  

RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&id=$2 [NC,QSA,L]  
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,QSA,L]

But it does not work completely when I'm trying to work with it locally.
Imagine that I have a directory myproject in htdocs (www) in my local web server path and other files are stored in this folder. Now I can see the project if I go to localhost/project.

Now I want to work with URLs.
It works well if I have only one parameter in URI like localhost/myproject/tours. But if I have 2 parameters like localhost/myprojects/tours/inside, it seems that all css, js and images files go away. I've also added RewriteBase /myproject to .htaccess file, but nothing solved.

What is my mistake? I need a solution that works on both remote and local server.


Solution

  • First of all, see my response on your other question about your code: Why .htaccess mod_rewrite does not show a real directory

    Now, RewriteBase won't solve your problem about css/js/images etc. It's only for htaccess and it defines the base path when a rule is rewritten.

    One common way to avoid this problem is to add in all your files a base url right after <head> html tag.
    For you, it would be: <base href="http://localhost/myproject/" />

    Otherwise, if you reach localhost/myprojects/tours/inside then your css/js/images links will be resolved as localhost/myprojects/tours/inside/__here__ because the default base path here is the current directory (/myproject/tours/inside/) and this is not what you want

    Edit: if that's the case, don't forget to remove leading slashes from your css/images/javascript html links