如何组织您的网站,以便 .htaccess 正常工作

发布于 2024-09-27 12:51:05 字数 1093 浏览 3 评论 0原文

我是第一次从头开始构建动态网站,所以我对此很陌生。

我需要将此 URL:

www.domain.org/world/2.html

与此匹配:

www.domain.org/index.php?html=world&rss=2

为此目的,我使用此重写规则:

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?html=$1&rss=$2 [L]

这应该有效,但事实并非如此!

如果我写:

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?html=$1&rss=$2 [R]

该网站将我重定向到正确的 URL,并且一切正常。

然而,当我再次将修饰符 [R] 更改为 [L] 时,我收到一个非常奇怪的页面。和我需要的一样,但是css文件没有加载。因此,内部 URL 与我需要的并不完全相同。当我回显 $_GET[html]$_GET[rss] 变量时,它们也是正确的。此外,当我从链接 www.domain.org/world/2.html 转到菜单中的另一个链接时,其构建如下: www.domain.org/world/2.html/environment.html,而不是 www.domain.org/environment.html

所以,我猜问题在于我的网站的组织。 .htacces 位于一个目录中。模板和 css 文件位于另一个目录中,处理不同页面的 .php 文件位于不同的目录中。但随后再次

www.domain.org/index.php?html=world&rss=2 

完美运行! .htaccess 可能会引导我到一些错误的目录,但我不知道如何修复它。

所以我真的很困惑到底问题出在哪里?!拜托,如果有人有线索,我将非常感激听到它。

感谢您的时间和考虑。

I am building a Dynamic web site from scratch for the first time so I am new at this.

I need to match this URL:

www.domain.org/world/2.html

with this:

www.domain.org/index.php?html=world&rss=2

For that purpose I am using this rewrite rule:

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?html=$1&rss=$2 [L]

And this should be working, but it is not!

If I write:

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?html=$1&rss=$2 [R]

the site redirects me to the right URL and everything is working properly.

However, when I change the modifier [R] to [L] again, I receive a very strange page. It is the same that I need, but the css file is not loaded. Consequently, the internal URL is not exactly the same with what I need. When I echo the $_GET[html] and $_GET[rss] variables, then they are the correct ones too. Additionally, when I go from link www.domain.org/world/2.html to another link from the menu, its builds like this:
www.domain.org/world/2.html/environment.html, instead of www.domain.org/environment.html

So, I guess that the problem is with the organization of my site. The .htacces is in one directory. The template and css file is in another directory and the .php files handling different pages are in different directories. But then again

www.domain.org/index.php?html=world&rss=2 

works perfectly!!! Probably the .htaccess leads me to some wrong directory, but I do not know how to fix it.

So I am really confused where is the problem?! Please, if somebody has a clue, I will be very grateful to hear it.

Thank you for your time and consideration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

与君绝 2024-10-04 12:51:05

但是,当我再次将修饰符 [R] 更改为 [L] 时,我收到一个非常奇怪的页面。和我需要的一样,但是css文件没有加载。

我猜您使用相对 URI 来引用样式表。像这样:

<link rel='stylesheet' href='../screen.css'>

由于浏览器认为它位于目录 /world/ 中,因此它在错误的目录中搜索。

解决方案:使用以 / 开头的绝对路径:

<link rel='stylesheet' href='/screen.css'>
<a href='/about/'>About us</a>

However, when I change the modifier [R] to [L] again, I receive a very strange page. It is the same that I need, but the css file is not loaded.

I guess you use relative URIs to reference the stylesheet. Like this:

<link rel='stylesheet' href='../screen.css'>

Since the browser thinks it is in the directory /world/ it searches in the wrong directory.

Solution: Use absolute paths starting with an /:

<link rel='stylesheet' href='/screen.css'>
<a href='/about/'>About us</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文