CSS 与漂亮 URL 的问题
我在 localhost/project 中有一个 index.php、profile.php 文件 我在 .htaccess 文件中编写了以下代码,通过访问 localhost/project/sam 来访问 localhost/project/profile.php?u=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
当我访问 >http://localhost/project/sam 它显示了我想要的内容,没有错误。但是当我访问 http://localhost/project/sam/ (带有尾随 / )时,它会显示纯文本页面,这意味着未附加 css 文件。
有人知道这是什么原因吗?并帮我解决这个问题?
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/project/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能正在使用相对 URI 路径来引用这些外部资源。如果没有另外指定,相对 URI 将在基本 URI 上解析,该基本 URI 是当前文档的 URI。
因此,当在
/project/sam
上使用像css/style.css
这样的相对 URI 路径时,它会解析为/project/css/style.css
>;但当在/project/sam/
上使用时,它会解析为/project/sam/css/style.css
。您可以通过使用绝对 URI 路径
/project/css/style.css
或使用BASE
元素。但请注意,后者将影响所有相对 URI,而不仅仅是相对 URI 路径。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 theBASE
element. But note that the latter will affect all relative URIs and not just relative URI paths.您正在使用 css 的相对链接。
You're using a relative link to your css.
你如何包含该 css 文件?如果您从 PHP 生成路径,并且您的请求包含尾随“/”,则该路径可能包含双“/”。请检查您的 HTML 输出并修改 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.
要么需要斜杠,要么禁止它,否则你永远不知道相对链接是否需要上层。例如,禁止斜杠:
这会将客户端重定向到不包含斜杠的同一页面。无论如何,最好为对象提供一个规范的 URI。
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:
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.