My css imges js path is changed after using of mod_rewrite

混江龙づ霸主 提交于 2019-12-24 08:59:26

问题


I have changed my url from

http://example.com/course.php?id=1001

To

http://example.com/course/1001

but after changing the url of all the css js and images is not working

I have use this code

RewriteEngine On
RewriteRule ^course/([A-Za-z0-9-]+)/?$ coursedetails.php?id=$1 [NC,L]

回答1:


one solution is that use absolute path (ex /css, or /js rather than just css/, /js but this is not looks a reliable solution since we've to change it on all files,

This is because your relative URIs have their base changed. Originally, the base is / when the page is /course.php?id=1001, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /course/1001 the base suddenly becomes /course/ and it tries to append that in front of all relative URLs and thus none of them load.

You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):

<base href="/">



回答2:


1st method

If those parts are hardcoded you could simply change your links adding ../ before :

path/to/my/assets/style.css

would become

../path/to/my/assets/style.css


2nd method

You could use .htaccess as Marc suggested (assuming your assets folder is assets :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^course/assets/(.*) assets/$1

Or using a redirection

RedirectMatch 303 /courses/assets(.*) /assets/$1

Note 303 See Other response code which seems more appropriate http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4



来源:https://stackoverflow.com/questions/31212900/my-css-imges-js-path-is-changed-after-using-of-mod-rewrite

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