dongtieshang5429
2015-03-02 16:02
浏览 116
已采纳

使用.htaccess从http重定向到https而不使用www

I have application based on PHP and Apache. If requested link is not directory or file, all requests go to index.php file. I wanna redirect all requests from http to https and without www.

Valid link for me:

  1. https://someaddress.org

Started from following links invalid for me(sorry my reputation is very small and i cant post more 2 links):

  1. http://
  2. http://www.
  3. www.

My htaccess looks like that

AddDefaultCharset UTF-8

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]

How i can do redirects to https and without www?

图片转代码服务由CSDN问答提供 功能建议

我有基于PHP和Apache的应用程序。 如果请求的链接不是目录或文件,则所有请求都将转到index.php文件。 我想将所有请求从http重定向到https而没有www。

我的有效链接:

  1. https://someaddress.org

    从以下链接开始对我无效( 抱歉,我的声誉非常小,我无法发布更多2个链接):

    1. http://
    2. http:// www
    3. www。

      我的htaccess看起来像那样< / p>

        AddDefaultCharset UTF-8 
       
      RewriteEngine on 
      RewriteBase / 
      RewriteCond%{REQUEST_FILENAME}!-d 
      RewriteCond%{REQUEST_FILENAME}!-f 
      RewriteRule ^(。*)  index.php [L] 
         
       
       

      如何重定向到https而没有www?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushijiao0679 2015-03-02 16:27
    已采纳

    Have another rule for http->http and www removal:

    AddDefaultCharset UTF-8
    
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://someaddress.org%{REQUEST_URI} [L,NE,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douwen1915 2015-03-02 16:57

    You can cover the http to https part with the SSLRequireSSL directive.

    <Directory />
      SSLRequireSSL
    </Directory>
    

    If the problem is the htaccess file and not the rewriting rules you can stop using the .htaccess file and move the rewriting rules to the apache configuration files.

    If you just dont want or can use rewriting rules, and as the vast majority of php applications needs a configuration file included in every single script (configuration vars or user control as examples) you can use this file to get the requested resource and strip the www. part if the domain name starts with them.

    I can provide a php code example if the include file may be the solution you need.

    评论
    解决 无用
    打赏 举报
  • douduonang3169 2018-07-18 18:27

    First redirect all request with WWW to https without WWW

    # For http://www.yourdomain.com and https://www.yourdomain.com
    RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
    RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]
    

    Now redirect all http request without WWW to https without WWWW

    # For http://yourdomain.com
    RewriteCond %{HTTPS} =off
    RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
    RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]
    

    Add above code one by one in .htacess file in sequance to redirect your all request to https without www

    I used "R=301" for permanent redirection

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题