【问题标题】:.htaccess let subdomain behave as main domain.htaccess 让子域充当主域
【发布时间】:2016-02-06 00:00:12
【问题描述】:

我想为一页主域创建子域。我不想复制文件,所以我尝试使用 mod_rewrite 解决它。

当我转到 submdomain.domain.com 时,我想将页面 xyz.html 显示为 index.html 以及所有图片。 mod重写应该将浏览器url中的地址保留为subdomain.domain.com

RewriteEngine On

RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteCond %{REQUEST_URI} !(index.html)
RewriteRule ^(.*) http://www.domain.com/$1 [P,L] 

RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteCond %{REQUEST_URI} index.html
RewriteRule index.html http://www.domain.com/xyz.html [P,L] 

这个.htaccess 保存在根目录的子域中。不幸的是,它只能单独工作。前半部分仅在后半部分缺失时有效,反之亦然。因此,我可以进行重定向以仅将 xyz.html 页面显示为索引页面但没有图像,或者对所有子域文件进行重定向,但忽略 xyz.html 并使用主域 index.html。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    最后我找到了解决办法

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^subdomain.domain.com
    RewriteCond %{REQUEST_URI} index.html
    RewriteRule index.html http://www.domain.com/myspecial-page.html [P,L] 
    
    RewriteCond %{HTTP_HOST} ^subdomain.domain.com
    RewriteRule ^([a-zA-Z0-9\.\-\_]+).html$ http://www.domain.com/$1.html [NC]
    
    RewriteCond %{HTTP_HOST} ^subdomain.domain.com
    RewriteRule ^images/([a-zA-Z0-9\.\-\_]+)$ http://www.domain.com/images/$1 [NC,P]
    
    etc...for css, js
    

    可以使用htaccess tester测试规则

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2016-07-06
      • 2023-03-10
      • 2014-12-09
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多