简体   繁体   中英

.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.

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

The browser will build absolute URL paths out of your relative URL paths by looking at your made up context of /myprojects/tours. You may need to strip one or two levels of that prefix off to find the real path.

The access log will show you plain as day what relative URL's come in when you use the old and new URLs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM