如何组织您的网站,以便 .htaccess 正常工作
我是第一次从头开始构建动态网站,所以我对此很陌生。
我需要将此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您使用相对 URI 来引用样式表。像这样:
由于浏览器认为它位于目录
/world/
中,因此它在错误的目录中搜索。解决方案:使用以
/
开头的绝对路径:I guess you use relative URIs to reference the stylesheet. Like this:
Since the browser thinks it is in the directory
/world/
it searches in the wrong directory.Solution: Use absolute paths starting with an
/
: