简体   繁体   中英

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

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/ ).

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

apache mod rewrite
one more
beginners guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM