【问题标题】:HTACCESS redirect .com.au to only .comHTACCESS 将 .com.au 重定向到仅 .com
【发布时间】:2021-04-05 05:44:05
【问题描述】:

以下是我的 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,L,NE]

现在我的问题是,我怎样才能使所有现有条件能够将 .com.au 重定向到 .com ?

有人可以指导我吗..

谢谢

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite url-rewriting


    【解决方案1】:

    您可以将两个域重定向到 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,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]
    

    【讨论】:

    • 非常感谢@anubhava .. 我会检查并回复你我的更新。谢谢。
    【解决方案2】:

    使用您展示的示例,请让您的 htaccess 遵循以下规则。 请确保在测试 URL 之前清除浏览器缓存。

    <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,R=301,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,R=301,L]
    
    ##NO https with www rules
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\.(rosterelf\.com)\.au$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,R=301,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>
    

    【讨论】:

    • 非常感谢@RavinderSingh13 .. 我会检查并回复你我的更新。谢谢。
    • @MittulAtTechnoBrave,嘿,Mittul,您好!!你能告诉我我的代码是否对你有用吗,很高兴知道这是否对你有帮助:) 干杯
    • 抱歉回复晚了..我没有登录..是的,你的回答帮助了我,我用你的代码解决了..非常感谢
    • @MittulAtTechnoBrave,欢迎您,非常乐意在这里提供帮助 :) 继续发帖,继续在这个伟大的网站上分享欢呼 :)
    猜你喜欢
    • 2021-05-02
    • 2016-11-17
    • 1970-01-01
    • 2013-06-13
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多