【问题标题】:How do i force www subdomain on both https and http我如何在 https 和 http 上强制 www 子域
【发布时间】:2010-12-02 12:52:06
【问题描述】:

无论出于何种原因,我似乎无法做到这一点,我在这里和 apache 的网站上查看了许多示例。我试图在 http 或 https 上强制使用 www.domain.com 而不是 domain.com,但我并没有试图通过 http 强制使用 https。

以下代码似乎适用于所有 https 连接,但 http 不会重定向到 www。

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301]

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301]

【问题讨论】:

标签: apache .htaccess mod-rewrite redirect


【解决方案1】:
  1. 您不需要第二个RewriteEngine 指令。这可能会或可能不会导致解析问题,从而使第二组规则不起作用。要测试是否是这种情况,请尝试切换您拥有的两个块的顺序。
  2. 最好使用 L 来修改绝对是最后的请求。因此,将 [R=301] 更改为 [R=301,L] 两次出现。
  3. 在很大程度上,作为风格问题,我会考虑将 RewriteRule 指令更改为类似(使用 http 或 https,视情况而定):

    RewriteRule ^(.*)$ @987654321@$1 [R=301,L,QSA]

【讨论】:

    【解决方案2】:

    您的规则似乎很好。您可以按如下方式组合它们:

    RewriteCond %{HTTP_HOST} !^www\.example\.com$
    RewriteCond %{HTTPS}s on(s)|
    RewriteRule ^ http%1://www.example.com%{REQUEST_URI} [L,R=301]
    

    还要注意附加的 L 标志,以在应用此规则后停止重写过程。

    【讨论】:

    • 这似乎应该有效,但仅适用于 https。我决定删除第二个条件,以便不管 https 是否没有 www 子域重写为 http。如果在 http 下,我制定的任何规则似乎都被忽略了。基本上任何 http 连接都忽略了这些规则。有什么想法吗?
    【解决方案3】:

    如果有人仍然需要这个问题的答案。使用另一个 .htaccess。从这里获取指南,我找到了,看起来不错:http://www.farinspace.com/codeigniter-htaccess-file/

    <IfModule mod_rewrite.c>
    
        RewriteEngine On
        RewriteBase /
    
        ### Canonicalize codeigniter URLs
    
        # If your default controller is something other than
        # "welcome" you should probably change this
        RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
        RewriteRule ^(.*)/index/?$ $1 [L,R=301]
    
        # Removes trailing slashes (prevents SEO duplicate content issues)
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.+)/$ $1 [L,R=301]
    
        # Enforce www
        # If you have subdomains, you can add them to 
        # the list using the "|" (OR) regex operator
        RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
        RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
    
        # Enforce NO www
        #RewriteCond %{HTTP_HOST} ^www [NC]
        #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
    
        ###
    
        # Removes access to the system folder by users.
        # Additionally this will allow you to create a System.php controller,
        # previously this would not have been possible.
        # 'system' can be replaced if you have renamed your system folder.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ /index.php/$1 [L]
    
        # Checks to see if the user is attempting to access a valid file,
        # such as an image or css document, if this isn't true it sends the
        # request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
    
    </IfModule>
    
    <IfModule !mod_rewrite.c>
    
        # Without mod_rewrite, route 404's to the front controller
        ErrorDocument 404 /index.php
    
    </IfModule>
    

    请记住,一旦您设置了 CodeIgniter htaccess 文件,您将需要进入“/system/application/config/config.php”,找到以下内容:

    $config['index_page'] = "index.php";
    

    并将其更改为:

    $config['index_page'] = "";
    

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      相关资源
      最近更新 更多