dongqiang5541 2019-08-13 08:36
浏览 150

.htaccess:如果还更改根路径,则将域重定向到https不起作用

I have the following domains:

example1.com
example2.com
example3.com

The domains point to /public_html/. There are three things I want to do in /public_html/.htaccess:

  1. Redirect (with all parameters and paths) the domains example2.com and example3.com to the domain example1.com.

  2. example1.com itself should get shown (if not, then redirect) always with https and www, means: https://www.example1.com

  3. The custom root path for the domains is /public_html/. I want to change this to /public_html/example_path/.

I have the following in /public_html/.htaccess:

RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example3.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^example1.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^example1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]

This is nearly working as expected. But when opening http://www.example1.com there is no redirection to https://www.example1.com. This is only working when removing the last four code lines, that should change the root path.

For all other domains it's working.

Why isn't it woking for http://www.example1.com? And why is it only working when not changing the root path?

  • 写回答

1条回答 默认 最新

  • dongyishe6689 2019-08-13 09:14
    关注

    Add this rule to yours at the top of the .htaccess file

    # http <> https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    

    Or check these rewrites instead of your rules:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    
        RewriteCond %{HTTP_HOST} !^www\.example1\.com [NC]
        RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    
        RewriteCond %{REQUEST_URI} !example_path/
        RewriteRule (.*) /example_path/$1 [L]
    
    </IfModule>
    

    For specific domains (www/non-www example-other.co, www/non-www example3.com, www/non-www example2.com, example1.com)

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # http www/non-www example-other.com/any to https://www.example-other.com/any
        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP_HOST} ^(www\.)?example-other\.com [NC]
        RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
    
        # https non-www example-other.com/any to https://www.example-other.com/any
        RewriteCond %{HTTP_HOST} ^example-other\.com [NC]
        RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
    
        # http www/non-www example1.com/any to https://www.example1.com/any
        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [NC]
        RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    
        # http/https www/non-www example2.com/any to https://www.example1.com/any
        RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
        RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    
        # http/https www/non-www example3.com/any to https://www.example1.com/any
        RewriteCond %{HTTP_HOST} ^(www\.)?example3\.com [NC]
        RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    
        # https non-www example1.com/any to https://www.example1.com/any
        RewriteCond %{HTTP_HOST} ^example1\.com [NC]
        RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
    
        # Rewrite /any to /example_path/any 
        RewriteCond %{REQUEST_URI} !example_path/
        RewriteRule (.*) /example_path/$1 [L]
    
    </IfModule>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)