URL mod_rewrite required

I am new to this mod_rewrite. I was able to successfully rewrite the URL something like this: http://mydomain.com/products/12 : http://mydomain.com/products.php?prodId=12

But when I specify http://mydomain.com/products/12 in the address bar, the css and js files are not loading.

But when I turn to http://mydomain.com/products.php?prodId=12 , js and css load correctly. Please let me know if I missed something.

My .htaccess looks like this:

 Options +FollowSymlinks RewriteEngine on RewriteRule ^products/([0-9][0-9])/$ /products.php?prodId=$1 [L] 

My css and js folders are also in the same folder.

+1
source share
2 answers

You need to know that relative URIs (thus absolute and relative paths of URIs too) are resolved from the base URI, which - if not explicitly declared - is the URI of the current document.

So, if you are referencing external resources from /products/12 with a relative URI foo/bar , it is allowed /products/foo/bar .

To fix this, use absolute URI paths (starting with / ) or absolute URIs (starting with protocol) or explicitly setting a base URI different from the current one (see the BASE HTML element ). But note that changing the base URI has some side effects, as it affects every relative URI.

+5
source

You should use relative links to your script / css files, for example:

 <script src="my.js"></script> 

This works for your old URL, but on the new one it will look in the "products" directory, which, of course, does not exist. Instead, you can use absolute URLs:

 <script src="/my.js"></script> 
+3
source

All Articles