mod_rewrite URL info required

余生长醉 提交于 2019-12-17 21:33:09

问题


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

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

But when I access http://mydomain.com/products.php?prodId=12 the js and css get loaded properly. Please let me know if I am missing 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:


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

So if you reference external resources from /products/12 with the relative URI foo/bar, it’s resolved to /products/foo/bar.

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




回答2:


You must be using relative links to your script/css files, like this:

<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 doesn't exist. You could use absolute urls instead:

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


来源:https://stackoverflow.com/questions/891775/mod-rewrite-url-info-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!