为什么我的php文件在使用RewriteRule时无法找到外部文件(image,css ...)?
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/ ).
我正在使用以下RewriteRules: p>
RewriteRule ^([ a-zA-Z0-9] +)$ page.php?p = $ 1
RewriteRule ^ e /([a-zA-Z0-9] +)$ edit.php?p = $ 1
code>
第一个工作正常:输入mysite.com/id时,它会在服务器端加载mysite.com/page.php?p=id。 p>
第二个也正常工作:当我输入mysite.com/e/id时,它会加载mysite.com/edit.php?p=id(正如预期的那样)。 但在这种情况下,edit.php不能像我的css文件那样删除任何外部文件。 p>
&lt; link rel =“stylesheet”type =“text / css”href = “style.css”/&gt;
code> pre>
执行以下任一操作时: p>
&lt; link rel =“stylesheet “type =”text / css“href =”../ style.css“/&gt;
code> pre>
或者只是删除RewriteRule中的目录,如: p>
RewriteRule ^ e _([a-zA-Z0-9] +)$ edit.php?p = $ 1
code> pre>
现在我不明白为什么我的edit.php无法找到外部文件,即使它被认为是在服务器端的正确路径上加载(mysite。 com /)而不是像额外目录中的url显示(在本例中为mysite.com/e/)。 p>
div>
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.
you have to write full path not the relative path when using mod_rewite
References