SubDomain.DOMAIN.COM在浏览器地址栏中保持不变 - .htaccess / mod_rewrite

时间:2012-02-14 06:39:58

标签: linux .htaccess

RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName/$1 [L]

如果我输入SubDomain.DOMAIN.COM,它会将我重定向到DOMAIN.COM/Folder/,但我不希望浏览器地址栏中的url更改为DOMAIN.COM/Folder/,但仍保留为SubDomain。 DOMAIN.COM。

任何线索。

2 个答案:

答案 0 :(得分:1)

RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName/$1 [L]

当rewriterule指向域时,将发生显式重定向。默认情况下为302重定向(临时重定向)。

我建议你使用P(代理)标志。要实现此目的,应启用mod_proxy

RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName/$1 [L,P]

还要记住设置ProxyReverse指令。

Context:    server config, virtual host, directory

因此,ProxyReverse中无法.htaccess

直接来自:Proxying Content with mod_rewrite Apache Docs
    考虑使用 ProxyPass ProxyPassMatch ,优先使用mod_rewrite。

访问此处了解如何 ProxyPass https://stackoverflow.com/a/9189447/858515

答案 1 :(得分:0)

看看RewriteRule docs

Absolute URL

If an absolute URL is specified, mod_rewrite checks to see whether the
hostname matches the current host. If it does, the scheme and hostname
are stripped out and the resulting path is treated as a URL-path.
Otherwise, an external redirect is performed for the given URL.

所以 - 尝试使用直接文件系统路径:

RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ /path/to/domain.com/DocumentRoot/FolderName/$1 [L]