CSS 与漂亮 URL 的问题

发布于 2024-10-06 10:05:22 字数 611 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾北清歌寒 2024-10-13 10:05:22

您可能正在使用相对 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 the BASE element. But note that the latter will affect all relative URIs and not just relative URI paths.

岁月静好 2024-10-13 10:05:22

您正在使用 css 的相对链接。

You're using a relative link to your css.

淡看悲欢离合 2024-10-13 10:05:22

你如何包含该 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.

野稚 2024-10-13 10:05:22

要么需要斜杠,要么禁止它,否则你永远不知道相对链接是否需要上层。例如,禁止斜杠:

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

这会将客户端重定向到不包含斜杠的同一页面。无论如何,最好为对象提供一个规范的 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文