隐形 mod_rewrite 并不总是隐形的!?(“www"和“无子域") [英] invisible mod_rewrite is not always invisible!? ("www" and "without subdomain")
问题描述
我的网站位于使用 cPanel 11 的主机上.
My site is on a host using cPanel 11.
不幸的是,它将www.e-motiv.net"和e-motiv.net"都重定向到了public_html.我想要public_html/www 和 public_html/这对最终用户是不可见的.
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.
我认为最好的方法是通过 mod_rewrite,所以我做了以下操作.
I thought the best way was through mod_rewrite, so I did the following.
文件空间看起来像这样(来自 public_html/):
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!):
- e-motiv.net ->/index.php
- www.e-motiv.net ->/www/index.php
- 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/嗯??
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??
如果 mod_rewrite 不是最好的解决方案,请告诉我!
If mod_rewrite is not the best solution, please do tell!
推荐答案
这是因为 mod_dir.mod_dir 将尾部斜线添加到映射到目录的 url.mod_dir 不知道这些使用 mod_rewrite 创建的虚拟网址".
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
然而,这会导致对 www.example.com/folder 的请求导致 404 未找到.不过,您可以通过一些重写规则来解决此问题.所以完整的解决方案将类似于:
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]
这篇关于隐形 mod_rewrite 并不总是隐形的!?(“www"和“无子域")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!