【问题标题】:.htaccess non www to no www SSL and www to www SSL.htaccess 非 www 到 no www SSL 和 www 到 www SSL
【发布时间】:2019-01-28 22:45:34
【问题描述】:

我到处搜索并尝试了各种.htaccess 配置,但没有找到答案。

只能访问我想要的.htaccess 文件:

将 NON www 直接连接到 NON www SSL 示例:http://example.com -> https://example.com

直接 www 到 www SSL 示例:http://www.example.com -> https://www.example.com

大多数示例显示强制所有对 SSL 的请求仅作为非 www 或 www。我两个都想要。

htaccess

<IfModule mod_headers.c>
  Header append Vary: User-Agent
  Header append Vary: Accept-Encoding
</IfModule>

AddDefaultCharset utf-8
AddType image/svg+xml svg

RewriteEngine On

RewriteRule ^sitemap\.xml$ /sitemap.php [L]

# Exception - Reached the document root then stop
# (Prevents rewrite loop if index.php is missing - 404 instead)
RewriteRule ^index\.php$ - [L]

# Exception - Any request for a valid file stop here
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# If already requesting "index.php" then step up a directory
RewriteRule ^(.*/)?[^/]+/index\.php$ /$1index.php [L]

# Otherwise try "index.php" in the current path segment
RewriteRule ^(.*/)?[^/]*$ /$1index.php [L]

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip
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
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilter DEFLATE .shtml
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On
AddType image/x-icon .ico
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>

【问题讨论】:

  • 然后强制HTTPs....
  • 看看RedirectPermanent(和其他重定向指令),对于简单的需求,它们已经足够了,你不需要mod_rewrite

标签: .htaccess ssl no-www


【解决方案1】:

你可以使用:

RewriteEngine on 
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] 

【讨论】:

  • 这样做似乎会创建一个无限重定向。添加了完整的 htaccess 文件以供参考。
猜你喜欢
  • 2014-11-24
  • 1970-01-01
  • 2016-04-02
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 2011-12-18
相关资源
最近更新 更多