.htaccess 使用Angular2的Htaccess强制SSL并修复路由

.htaccess 使用Angular2的Htaccess强制SSL并修复路由,.htaccess,.htaccess,因此,我的NG2应用程序有两个问题需要解决: 刷新路由时,我得到一个页面未找到错误 当访问非SSL路由时,我希望重新指向SSL 这修复了损坏的路由: RewriteEngine on RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^(.*)

因此,我的NG2应用程序有两个问题需要解决:

  • 刷新路由时,我得到一个页面未找到错误
  • 当访问非SSL路由时,我希望重新指向SSL
  • 这修复了损坏的路由:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
    
    这将强制SSL。但是,当包含在上述代码中时,它将不再工作。

    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
    RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    

    如何让这两个条件一起工作?

    我也遇到了同样的问题,我做了一个变通,这样当页面加载到http上时,它就可以用https代替http。我知道它不是很优雅,它会加载页面两次,但我想分享一下。也许有人可以帮助您实现纯htaccess解决方案

    这是我的app.component.ts

    import { Location } from '@angular/common';
    import { Component, OnInit } from '@angular/core';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    export class AppComponent implements OnInit {
    
        location: Location;
    
        ngOnInit() {
            if (location.protocol === 'http:') {
                window.location.href = location.href.replace('http', 'https');
            }
        }
    }
    

    我也遇到了同样的问题,我做了一个变通,当页面加载到http上时,它只是用https替换http。我知道它不是很优雅,它会加载页面两次,但我想分享一下。也许有人可以帮助您实现纯htaccess解决方案

    这是我的app.component.ts

    import { Location } from '@angular/common';
    import { Component, OnInit } from '@angular/core';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    export class AppComponent implements OnInit {
    
        location: Location;
    
        ngOnInit() {
            if (location.protocol === 'http:') {
                window.location.href = location.href.replace('http', 'https');
            }
        }
    }
    

    这是我的最终解决方案

    这包括兑现,Gzip等,使您的应用程序超级快速

    #REDIRECT ROUTES TO INDEX (fixes broken routes with angular)
    RewriteEngine on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
    #ENABLE GZIP COMPRESSION TO IMPROVE PERFORMANCE
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    # SET EXPIRE HEADERS TO IMPROVE PERFORMANCE
    <ifModule mod_expires.c>
      ExpiresActive On
      ExpiresDefault "access plus 2 days"
      ExpiresByType image/x-icon "access plus 1 year"
      ExpiresByType image/jpeg "access plus 1 year"
      ExpiresByType image/jpg "access plus 1 year"
      ExpiresByType image/png "access plus 1 year"
      ExpiresByType image/gif "access plus 1 year"
      ExpiresByType application/x-shockwave-flash "access plus 1 month"
      ExpiresByType text/css "access plus 1 month"
      ExpiresByType text/javascript "access plus 1 month"
      ExpiresByType application/pdf "access plus 1 month"
      ExpiresByType application/javascript "access plus 2 week"
      ExpiresByType application/x-javascript "access plus 2 week"
      ExpiresByType text/javascript "access plus 2 week"
      ExpiresByType text/html "access plus 600 seconds"
      ExpiresByType application/xhtml+xml "access plus 600 seconds"
    </ifModule>
    # END Expire headers
    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
      <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(css)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(js)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
      </filesMatch>
    </ifModule>
    # END Cache-Control Headers
    
    #将路由重定向到索引(使用角点修复损坏的路由)
    重新启动发动机
    重写cond%{HTTP:X-Forwarded-Proto}!https
    重写条件%{HTTPS}关闭
    重写规则^https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301,NE]
    RewriteCond%{REQUEST_FILENAME}-s[或]
    RewriteCond%{REQUEST_FILENAME}-l[或]
    RewriteCond%{REQUEST_FILENAME}-d
    重写规则^.*$-[NC,L]
    重写规则^(.*)/index.html[NC,L]
    #启用GZIP压缩以提高性能
    AddOutputFilterByType泄气文本/普通
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE应用程序/xml
    AddOutputFilterByType DEFLATE应用程序/xhtml+xml
    AddOutputFilterByType DEFLATE应用程序/rss+xml
    AddOutputFilterByType DEFLATE应用程序/javascript
    AddOutputFilterByType DEFLATE应用程序/x-javascript
    #设置过期标头以提高性能
    过期于
    ExpiresDefault“访问加2天”
    过期按类型图像/x图标“访问加1年”
    过期按类型图像/jpeg“访问加1年”
    过期按类型映像/jpg“访问加1年”
    过期按类型图像/png“访问加1年”
    ExpiresByType image/gif“访问加1年”
    过期按类型应用程序/x-shockwave-flash“访问加1个月”
    ExpiresByType文本/css“访问加1个月”
    ExpiresByType文本/javascript“访问加1个月”
    过期按类型应用程序/pdf“访问加1个月”
    ExpiresByType应用程序/javascript“访问加2周”
    ExpiresByType应用程序/x-javascript“访问加2周”
    ExpiresByType文本/javascript“访问加2周”
    ExpiresByType text/html“访问加600秒”
    ExpiresByType应用程序/xhtml+xml“访问加600秒”
    #结束过期标头
    #开始缓存控制头
    标头集缓存控制“公共”
    标头集缓存控制“公共”
    标头集缓存控制“公共”
    标题集缓存控制“专用,必须重新验证”
    #结束缓存控制头
    
    这是我的最终解决方案

    这包括兑现,Gzip等,使您的应用程序超级快速

    #REDIRECT ROUTES TO INDEX (fixes broken routes with angular)
    RewriteEngine on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
    #ENABLE GZIP COMPRESSION TO IMPROVE PERFORMANCE
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    # SET EXPIRE HEADERS TO IMPROVE PERFORMANCE
    <ifModule mod_expires.c>
      ExpiresActive On
      ExpiresDefault "access plus 2 days"
      ExpiresByType image/x-icon "access plus 1 year"
      ExpiresByType image/jpeg "access plus 1 year"
      ExpiresByType image/jpg "access plus 1 year"
      ExpiresByType image/png "access plus 1 year"
      ExpiresByType image/gif "access plus 1 year"
      ExpiresByType application/x-shockwave-flash "access plus 1 month"
      ExpiresByType text/css "access plus 1 month"
      ExpiresByType text/javascript "access plus 1 month"
      ExpiresByType application/pdf "access plus 1 month"
      ExpiresByType application/javascript "access plus 2 week"
      ExpiresByType application/x-javascript "access plus 2 week"
      ExpiresByType text/javascript "access plus 2 week"
      ExpiresByType text/html "access plus 600 seconds"
      ExpiresByType application/xhtml+xml "access plus 600 seconds"
    </ifModule>
    # END Expire headers
    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
      <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(css)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(js)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
      </filesMatch>
    </ifModule>
    # END Cache-Control Headers
    
    #将路由重定向到索引(使用角点修复损坏的路由)
    重新启动发动机
    重写cond%{HTTP:X-Forwarded-Proto}!https
    重写条件%{HTTPS}关闭
    重写规则^https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301,NE]
    RewriteCond%{REQUEST_FILENAME}-s[或]
    RewriteCond%{REQUEST_FILENAME}-l[或]
    RewriteCond%{REQUEST_FILENAME}-d
    重写规则^.*$-[NC,L]
    重写规则^(.*)/index.html[NC,L]
    #启用GZIP压缩以提高性能
    AddOutputFilterByType泄气文本/普通
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE应用程序/xml
    AddOutputFilterByType DEFLATE应用程序/xhtml+xml
    AddOutputFilterByType DEFLATE应用程序/rss+xml
    AddOutputFilterByType DEFLATE应用程序/javascript
    AddOutputFilterByType DEFLATE应用程序/x-javascript
    #设置过期标头以提高性能
    过期于
    ExpiresDefault“访问加2天”
    过期按类型图像/x图标“访问加1年”
    过期按类型图像/jpeg“访问加1年”
    过期按类型映像/jpg“访问加1年”
    过期按类型图像/png“访问加1年”
    ExpiresByType image/gif“访问加1年”
    过期按类型应用程序/x-shockwave-flash“访问加1个月”
    ExpiresByType文本/css“访问加1个月”
    ExpiresByType文本/javascript“访问加1个月”
    过期按类型应用程序/pdf“访问加1个月”
    ExpiresByType应用程序/javascript“访问加2周”
    ExpiresByType应用程序/x-javascript“访问加2周”
    ExpiresByType文本/javascript“访问加2周”
    ExpiresByType text/html“访问加600秒”
    ExpiresByType应用程序/xhtml+xml“访问加600秒”
    #结束过期标头
    #开始缓存控制头
    标头集缓存控制“公共”
    标头集缓存控制“公共”
    标头集缓存控制“公共”
    标题集缓存控制“专用,必须重新验证”
    #结束缓存控制头