微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HTACCESS 将 .com.au 重定向到仅 .com

如何解决HTACCESS 将 .com.au 重定向到仅 .com

下面是我的 HTACCESS,我使用以下规则重定向我的网站。

https://rosterelf.com 将被重定向https://www.rosterelf.com/

http://www.rosterelf.com 将被重定向https://www.rosterelf.com

rosterelf.com 将被重定向https://www.rosterelf.com/

所以基本上它会检查 url 是否没有 wwwhttps 然后它会直接使用它。这对我来说很好用。

但是,现在我想用 .com.au 重定向我的所有网址 ..

我应该能够以上述相同的条件将 https://www.rosterelf.com.au/ 重定向https://www.rosterelf.com/。通过检查 https非 www 到 www

我尝试了各种代码,例如

RewriteCond %{HTTP_HOST} =m.somesite.com.au
RewriteRule (.*) http://www.somesite.com.au/$1 [R,L]

和其他人通过添加我现有的 htaccess 文件,但它在少数情况下不起作用,例如在我的内页 .. 或没有 https。这是我当前的 htaccess 文件的样子。

<IfModule mod_rewrite.c>

    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    

    RewriteEngine On

    ##
    ## You may need to uncomment the following line for some hosting environments,## if you have installed to a subdirectory,enter the name here also.
    ##
    # RewriteBase /

    ##
    ## Uncomment following lines to force HTTPS.
    ##
    # RewriteCond %{HTTPS} off
    # RewriteRule (.*) https://%{SERVER_NAME}/$1 [L,R=301]
    
    # CONDITIONS FOR ONLY LIVE SITE STARTS 
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    # CONDITIONS FOR ONLY LIVE SITE ENDS  
    
    


    ##
    ## Black listed folders
    ##
    RewriteRule ^bootstrap/.* index.PHP [L,NC]
    RewriteRule ^config/.* index.PHP [L,NC]
    RewriteRule ^vendor/.* index.PHP [L,NC]
    RewriteRule ^storage/cms/.* index.PHP [L,NC]
    RewriteRule ^storage/logs/.* index.PHP [L,NC]
    RewriteRule ^storage/framework/.* index.PHP [L,NC]
    RewriteRule ^storage/temp/protected/.* index.PHP [L,NC]
    RewriteRule ^storage/app/uploads/protected/.* index.PHP [L,NC]

    ##
    ## White listed folders
    ##
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} !/.well-kNown/*
    RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/public/.*
    RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
    RewriteCond %{REQUEST_FILENAME} !/storage/app/resized/.*
    RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
    RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
    RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
    RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
    RewriteRule !^index.PHP index.PHP [L,NC]

    ##
    ## Block all PHP files,except index
    ##
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} \.PHP$
    RewriteRule !^index.PHP index.PHP [L,NC]

    ##
    ## Standard routes
    ##
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.PHP [L]

</IfModule>

正如您在 htaccess 上面看到的,我使用了以下 3 行代码将非 www 重定向到 www 并将 http 重定向到 https ..

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE]

现在我的问题是,如何使所有现有条件都能将 .com.au 重定向到 .com ?

有人可以指导我吗..

谢谢

解决方法

使用您显示的示例,请按照以下规则进行 htaccess。 请确保在测试您的网址之前清除浏览器缓存。

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
  Options -MultiViews
</IfModule>

RewriteEngine ON
RewriteBase /

##NO https,no www rules
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^rosterelf\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

##NO https with www rules
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.rosterelf\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L]


##NO https,no www rules
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(rosterelf\.com)\.au$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L]

##NO https with www rules
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(rosterelf\.com)\.au$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L]

## Black listed folders
##
RewriteRule ^(bootstrap|config|vendor)/.* index.php [L,NC]
RewriteRule ^storage/(cms|logs|framework)/.* index.php [L,NC]
RewriteRule ^storage/temp/protected/.* index.php [L,NC]
RewriteRule ^storage/app/uploads/protected/.* index.php [L,NC]

##
## White listed folders
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/.well-known/*
RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/public/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/resized/.*
RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
RewriteRule !^index.php index.php [L,NC]

##
## Block all PHP files,except index
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.php$ [NC]
RewriteRule !^index\.php index.php [L,NC]

##
## Standard routes
##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
,

您可以将两个域重定向到 https 并在单个重定向规则中添加 www.。确保在其他规则之前保留这个新的重定向规则:

RewriteEngine On

## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# other rules come below this line

##
## Black listed folders
##
RewriteRule ^bootstrap/.* index.php [L,NC]
RewriteRule ^config/.* index.php [L,NC]
RewriteRule ^vendor/.* index.php [L,NC]
RewriteRule ^storage/cms/.* index.php [L,NC]
RewriteRule ^storage/logs/.* index.php [L,NC]
RewriteRule ^storage/framework/.* index.php [L,except index
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule !^index.php index.php [L,NC]

##
## Standard routes
##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。