2014-11-25 153 views
0

我想实现让子域指向子文件夹,例如 http://{subdomain}.example.com指向http://example.com/{subdomain}而不更改地址栏中的URL。htaccess重写子域到子文件夹

我现在有这样的代码:

RewriteEngine on 
RewriteBase/
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com 
RewriteRule ^(.*)$ http://example.com/%1 [L] 

这似乎是工作,但它改变了我的地址栏藏汉

我knnow有关于这个问题多,但没有装我awnser。

回答

1

尝试使用virtualhosts代替,编辑您的httpd-vhosts.conf和补充一点:

<VirtualHost *:80> 
    ServerAlias *.yourdomain.com #wildcard catch all 
    VirtualDocumentRoot /opt/lampp/htdocs/%1 
    UseCanonicalName Off 
    <Directory "opt/lampp/htdocs"> 
    Options FollowSymLinks 
    AllowOverride All 
    Order allow,deny 
    Allow from all 
    </Directory> 
</VirtualHost> 
+0

这解决了我的问题,谢谢。 – Derp 2014-11-25 12:38:40

1

你可以在你DOCUMENT_ROOT/.htaccess文件中使用此前瞻基于规则:

RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_HOST} !^www\. 
RewriteCond %{HTTP_HOST} ^.*\.example\.com$ [NC] 
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$ 
RewriteRule^%1%{REQUEST_URI} [L] 
+0

这并没有解决我的问题,它的行为与我在我的问题中所表达的完全一致。感谢您的帮助,虽然:) – Derp 2014-11-25 12:38:19

+0

这个规则在我的web服务器上工作得很好。如果它正在更改URL,那么问题可能是您的浏览器缓存。 – anubhava 2014-11-25 14:39:32