.htaccess 通用htaccess重写:始终使用https和www
我正在搜索一个通用的htaccess重写,它强制用户使用https和www。因此,以下URL应全部重写为:.htaccess 通用htaccess重写:始终使用https和www,.htaccess,.htaccess,我正在搜索一个通用的htaccess重写,它强制用户使用https和www。因此,以下URL应全部重写为: 它应该是一个通用的解决方案,以便该域名不包含在规则中。最有可能的是,规则将包含类似于%{HTTP\u HOST}%{REQUEST\u URI}或%1/$1的内容。请描述您为什么使用其中一种 虽然有很多类似的问题,但我没有找到一个结合了https和www重定向的解决方案。希望你能帮忙 编辑1: 我尝试了@anubhava的解决方案,但是我遇到了一个“重定向太多”的错误。由于该站
%{HTTP\u HOST}%{REQUEST\u URI}
或%1/$1
的内容。请描述您为什么使用其中一种
虽然有很多类似的问题,但我没有找到一个结合了https和www重定向的解决方案。希望你能帮忙
编辑1:
我尝试了@anubhava的解决方案,但是我遇到了一个“重定向太多”的错误。由于该站点是Magento站点,因此htaccess的访问稍微困难一些。以下是我尝试过的两个mod_重写部分:
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
############################################
## you can put here your magento root folder
## path relative to web root
#RewriteBase /magento/
############################################
## uncomment next line to enable light API calls processing
# RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
############################################
## rewrite API2 calls to api.php (by now it is REST only)
RewriteRule ^api/rest api.php?type=rest [QSA,L]
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
############################################
## redirect for mobile user agents
#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
############################################
##启用重写
选项+FollowSymLinks
重新启动发动机
重写cond%{HTTP_HOST}^www\。[北卡罗来纳州]
重写规则^https://www.%{HTTP_HOST}%{REQUEST_URI}[R=301,L]
重写条件%{HTTPS}关闭
重写规则^https://%{HTTP_HOST}%{REQUEST_URI}[R=301,L]
############################################
##您可以将magento根文件夹放在这里
##相对于web根目录的路径
#重新编写BASE/magento/
############################################
##取消注释下一行以启用轻API调用处理
#重写规则^api/([a-z][0-9a-z]+)/?$api.php?type=$1[QSA,L]
############################################
##重写对api.php的API2调用(现在只剩下REST)
重写规则^api/restapi.php?type=rest[QSA,L]
############################################
##HTTP授权的变通方法
##在CGI环境中
重写规则。*-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]
############################################
##禁用跟踪和跟踪HTTP方法以防止XSS攻击
RewriteCond%{REQUEST_METHOD}^TRAC[EK]
重写规则。*-[L,R=405]
############################################
##移动用户代理重定向
#重写cond%{REQUEST_URI}^/mobiledirectoryhere/*$
#重写Cond%{HTTP|u USER|u AGENT}“android |黑莓| ipad | iphone | ipod | iemobile | opera mobile | palmos | webos | googlebot mobile”[NC]
#重写规则^(.*)$/mobiledirectoryhere/[L,R=302]
############################################
##在这些文件夹中丢失文件时始终发送404
重写cond%{REQUEST_URI}^/(媒体|皮肤| js)/
############################################
##决不重写现有文件、目录和链接
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-L
############################################
##将所有其他内容重写为index.php
重写规则。*index.php[L]
第二个版本:
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,L]
############################################
## you can put here your magento root folder
## path relative to web root
#RewriteBase /magento/
############################################
## uncomment next line to enable light API calls processing
# RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
############################################
## rewrite API2 calls to api.php (by now it is REST only)
RewriteRule ^api/rest api.php?type=rest [QSA,L]
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
############################################
## redirect for mobile user agents
#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
############################################
##启用重写
选项+FollowSymLinks
重新启动发动机
重写cond%{HTTPS}关闭[或]
重写cond%{HTTP_HOST}^www\。
重写规则^https://www.domain.tld%{REQUEST_URI}[R=301,L]
############################################
##您可以将magento根文件夹放在这里
##相对于web根目录的路径
#重新编写BASE/magento/
############################################
##取消注释下一行以启用轻API调用处理
#重写规则^api/([a-z][0-9a-z]+)/?$api.php?type=$1[QSA,L]
############################################
##重写对api.php的API2调用(现在只剩下REST)
重写规则^api/restapi.php?type=rest[QSA,L]
############################################
##HTTP授权的变通方法
##在CGI环境中
重写规则。*-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]
############################################
##禁用跟踪和跟踪HTTP方法以防止XSS攻击
RewriteCond%{REQUEST_METHOD}^TRAC[EK]
重写规则。*-[L,R=405]
############################################
##移动用户代理重定向
#重写cond%{REQUEST_URI}^/mobiledirectoryhere/*$
#重写Cond%{HTTP|u USER|u AGENT}“android |黑莓| ipad | iphone | ipod | iemobile | opera mobile | palmos | webos | googlebot mobile”[NC]
#重写规则^(.*)$/mobiledirectoryhere/[L,R=302]
############################################
##在这些文件夹中丢失文件时始终发送404
重写cond%{REQUEST_URI}^/(媒体|皮肤| js)/
############################################
##决不重写现有文件、目录和链接
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-L
############################################
##将所有其他内容重写为index.php
重写规则。*index.php[L]
有什么想法吗
编辑2:
仅仅按照@anubhava的建议,将Magento设置为对安全和不安全的基本URL使用https并不能解决问题。然后将URL重定向到而不是。您可以尝试以下操作:
<IfModule mod_rewrite.c>
RewriteEngine On
# non www to https://www...
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# non https to http
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
重新启动发动机
#非www-tohttps://www...
重写cond%{HTTP_HOST}^www\。
重写规则^(.*)$https://www.%{HTTP_HOST}/$1[R=301,L]
#非https到http
重写条件%{HTTPS}关闭
重写规则^(.*)$https://%{HTTP_HOST}/$1[R=301,L]
第一个重写规则检查主机名是否以www开头,如果不是,则使用https重定向并将www前置到主机名。标志R=301指示apache使用HTTP 301永久重定向,L导致mod_rewrite停止处理规则集。有关详细信息,请参阅文档:
第二个重写规则捕获带有www但不带https://的请求。主机名和路径保持不变。感谢Nicolás Echániz和Joseph Scott在上的博客指出了第二条规则。清除浏览器缓存,然后首先重新启动浏览器 Magento设置c
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]