不可见的 mod_rewrite 并不总是不可见的!? (“www”和“无子域”)

发布于 2025-01-01 02:15:31 字数 851 浏览 0 评论 0原文

我的网站位于使用 cPanel 11 的主机上。

不幸的是,它将 “www.e-motiv.net”和“e-motiv.net” 重定向到 public_html。 我想要回复。 public_html/www 和 public_html/ 这对最终用户不可见。

我认为最好的方法是通过 mod_rewrite,所以我做了以下操作。

文件空间看起来像这样(来自 public_html/):

  • /.htaccess
  • /index.php
  • /www/index.html
  • /www/test/index.html

我想要这个(第二部分不可见!):

  1. e-motiv.net -> ; /index.php
  2. www.e-motiv.net -> /www/index.php
  3. www.e-motiv.net/test -> /www/test/index.php

我认为这可以做到:

RewriteCond %{HTTP_HOST} ^www.e-motiv.net$
RewriteCond %{REQUEST_URI} !/www
RewriteRule ^(.*)$ /www/$1  [NC,L]

1和2工作,但是虽然3给出了正确的文件,但它改变了地址!? (所以不是看不见的) 因此,在地址栏中您会看到:www.e-motiv.net/test -> www.e-motiv.net/www/test/ 啊??

如果 mod_rewrite 不是最好的解决方案,请告诉我们!

My site is on a host using cPanel 11.

Unfortunatly it redirects both "www.e-motiv.net" and "e-motiv.net" to public_html.
I want resp. public_html/www and public_html/ and this invisible to the end user.

I thought the best way was through mod_rewrite, so I did the following.

File space looks like this (from public_html/):

  • /.htaccess
  • /index.php
  • /www/index.html
  • /www/test/index.html

And I want this (second part invisible!):

  1. e-motiv.net -> /index.php
  2. www.e-motiv.net -> /www/index.php
  3. www.e-motiv.net/test -> /www/test/index.php

I thought this would do it:

RewriteCond %{HTTP_HOST} ^www.e-motiv.net$
RewriteCond %{REQUEST_URI} !/www
RewriteRule ^(.*)$ /www/$1  [NC,L]

1 and 2 work, but although 3 gives the right file, it changes the address!? (so not invisible)
So, in address bar you get: www.e-motiv.net/test -> www.e-motiv.net/www/test/
Huh??

If mod_rewrite is not the best solution, please do tell!

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

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

发布评论

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

评论(1

少女净妖师 2025-01-08 02:15:31

这是因为 mod_dir。 mod_dir 将尾部斜杠添加到映射到目录的 url 中。 mod_dir 不知道这些使用 mod_rewrite 创建的“虚拟 URL”。

因此,要么通过使用禁用此行为,

DirectorySlash Off

但是,这将向 www.example.com/folder 发出请求,导致找不到 404。不过,您可以通过一些重写规则来解决这个问题。所以完整的解决方案是这样的:

DirectorySlash Off
#www dir only
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^www/(.+[^/])$ /$1/ [R,L]
#other dirs
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^(.+[^/])$ /$1/ [R,L]

This is because of mod_dir. mod_dir adds the tailing slashed to urls that map to directories. mod_dir is not aware of these 'virtual urls' created with mod_rewrite.

So either disable this behavior by using

DirectorySlash Off

This will however make requests to www.example.com/folder result in a 404 not found. You can fix this with some rewriterule though. So the complete solution would be something like:

DirectorySlash Off
#www dir only
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^www/(.+[^/])$ /$1/ [R,L]
#other dirs
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^(.+[^/])$ /$1/ [R,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文