Why is my php file unable to locate external files (image,css…) when using RewriteRule?

自古美人都是妖i 提交于 2019-12-25 04:59:10

问题


I am using following RewriteRules :

RewriteRule ^([a-zA-Z0-9]+)$ page.php?p=$1
RewriteRule ^e/([a-zA-Z0-9]+)$ edit.php?p=$1

The first one works fine : when typing mysite.com/id it loads mysite.com/page.php?p=id on server side.

The second one is working as well : when I type mysite.com/e/id it loads mysite.com/edit.php?p=id (as expected). But in that case edit.php can't loacte any external files like my css file.

<link rel="stylesheet" type="text/css" href="style.css" />

When doing either :

<link rel="stylesheet" type="text/css" href="../style.css" />

Or simply removing the direcory in the RewriteRule like :

RewriteRule ^e_([a-zA-Z0-9]+)$ edit.php?p=$1

it fixes that problem.

Now I don't understand why my edit.php is unable to locate external files even thought it loads on the correct path on server-side (mysite.com/) and not like the url displays in an extra directory (in this case mysite.com/e/ ).


回答1:


You can use a base url for all the site's paths, like this:

<base href="http://yoursite.com/blog/" />

or

<base href="/blog/" />

And then writing relative paths from the base's:

<a href="rule/foo/">...</a>

Plus, you have to be extra careful with those "accept all" patterns, as they may conflict with the stylesheets, images and the rest of media if not used properly. Either appending an extension to the patterns or excluding media could be possible fixes.




回答2:


you have to write full path not the relative path when using mod_rewite

References

apache mod rewrite
one more
beginners guide



来源:https://stackoverflow.com/questions/11411131/why-is-my-php-file-unable-to-locate-external-files-image-css-when-using-rew

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