开发者

CSS trouble with Pretty URLs

开发者 https://www.devze.com 2023-01-28 05:05 出处:网络
I have an index.php, profile.php files in localhost/project I\'ve written following code in my .htaccess file to have localhost/project/profile.php?u=sam by visiting localhost/project/sam

I have an index.php, profile.php files in localhost/project I've written following code in my .htaccess file to have localhost/project/profile.php?u=sam by visiting localhost/project/sam

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?u=$1

When I visit http://localhost/pr开发者_如何学Gooject/sam it diplays what I want without errors. But when I visit http://localhost/project/sam/ (with trailing / ) it displays the page with plain text, that means the css file is not attached.

Anyone know the reason for this? And help me to solve this?


You are probably using relative URI paths to reference those external resources. And relative URIs are resolved on a base URI that is the URI of the current document if not specified otherwise.

So when using a relative URI path like css/style.css on /project/sam it is resolved to /project/css/style.css; but when used on /project/sam/ it is resolved to /project/sam/css/style.css.

You could fix this by either using the absolute URI path /project/css/style.css or by changing the base URI with the BASE element. But note that the latter will affect all relative URIs and not just relative URI paths.


You're using a relative link to your css.


How do you include that css file? If you generate the path from PHP, it might contain a double '/' if your request contains a trailing '/'. Please check your HTML output and moidify the path of the CSS.


Either require the slash, or forbid it, or you'll never know whether relative links need to go uplevel or not. For example to forbid the slash:

RewriteRule ^(.*)/$ $1 [R]

which will redirect the client to the same page without the slash included. It's best to have a single canonical URI for an object anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消