使用RewriteRule时,为什么我的php文件无法找到外部文件(图像,css ...)?
我正在使用以下RewriteRules:
I am using following RewriteRules :
RewriteRule ^([a-zA-Z0-9]+)$ page.php?p=$1
RewriteRule ^e/([a-zA-Z0-9]+)$ edit.php?p=$1
第一个可以正常工作:输入mysite.com/id时,它将在服务器端加载mysite.com/page.php?p=id.
The first one works fine : when typing mysite.com/id it loads mysite.com/page.php?p=id on server side.
第二个也可以正常工作:当我键入mysite.com/e/id时,它会加载mysite.com/edit.php?p=id(如预期的那样).但是在那种情况下,edit.php无法定位任何外部文件,例如我的css文件.
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" />
两者之一时:
<link rel="stylesheet" type="text/css" href="../style.css" />
或者像这样简单地删除RewriteRule中的目录:
Or simply removing the direcory in the RewriteRule like :
RewriteRule ^e_([a-zA-Z0-9]+)$ edit.php?p=$1
它解决了这个问题.
现在,我什至不明白为什么我的edit.php无法定位外部文件,甚至认为它会加载在服务器端(mysite.com/)的正确路径上,而不像url显示在额外目录中(在这种情况下为mysite.com/e/).
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/ ).
您可以为网站的所有路径使用基本URL,如下所示:
You can use a base url for all the site's paths, like this:
<base href="http://yoursite.com/blog/" />
或
<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.