【问题标题】:Want to redirect TLD domain to a subdomain on https with htaccess. Tried htaccess RewriteRule Redirect found but not working想要使用 htaccess 将 TLD 域重定向到 https 上的子域。尝试找到 htaccess RewriteRule 重定向但不工作
【发布时间】:2018-01-27 10:51:42
【问题描述】:

我想通过 .htaccess 指令重定向所有这些:

http://domain.com AND www.domain.com AND http://子域.domain.com

到:

https://子域.domain.com

请注意,“子域”是一个唯一的子域,我不想重定向任何子域,只想重定向一个。

我不熟悉 .htaccess 指令,所以我尝试了所有在此处或其他地方找到的 RewriteRule 或 Redirect sn-p,但它不起作用。我一定错过了什么。

编辑:我在子域的 sur 目录中添加了一个 .htaccess,它现在可以工作了: 没关系,我在htaccess里把这个加到subdomain的子目录下:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite http-status-code-301


    【解决方案1】:

    尝试:

    # for main domain
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
    
    # for sub domain
    RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
    

    【讨论】:

    • 在尝试之前,请清除缓存或使用其他浏览器。
    • 我尝试过(在缓存清理后并确保使用两个浏览器)它重定向 http://domain.com 和 http: //www.domain.com 到 https://subdomain.domain.com 但不要将 http://subdomain.domain.com 重定向到 https://subdomain.domain.com。我在子域文件夹中有另一个 .htaccess - 用于 url 格式,也许它阻止重定向?
    • .htacess 进入子域文件夹的内容是:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]
    • 没关系,我在htaccess中添加了这个到子域的子目录中:RewriteCond %{HTTPS} off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]现在可以使用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多