1

我正在寻找一个通用的htaccess 重写,它强制用户使用 httpswww。因此,以下 URL 应全部重写为https://www.domain.tld/some/path

它应该是一个通用的解决方案,以便域名不包含在规则中。最有可能的是,该规则将包含类似%{HTTP_HOST}%{REQUEST_URI}or的内容%1/$1。请描述您为什么使用其中一种。

尽管有很多与此类似的问题,但我没有找到结合了 https 和 www 重定向的解决方案。希望你能帮忙。

编辑1:

我尝试了@anubhava 的解决方案,但出现“重定向过多”错误。由于该站点是 Magento 站点,因此 htaccess 稍微困难一些。以下是我尝试过的两个 mod_rewrite 部分:

<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>

第二个版本:

<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>

有任何想法吗?

编辑2:

正如@anubhava 所建议的那样,仅将 Magento 设置为使用 https 作为安全和不安全的基本 URL 并不能解决问题。然后 URL http://www.domain.tld/some/path被重定向到https://www.domain.tld而不是https://www.domain.tld/some/path

4

2 回答 2

1

你可以试试这个:

<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 开头,如果不是,则使用 https 重定向并将 www 附加到主机名。标志 R=301 指示 apache 使用 HTTP 301 永久重定向,L 导致 mod_rewrite 停止处理规则集。有关详细信息,请查看文档:http ://httpd.apache.org/docs/current/rewrite/

第二个重写规则捕获带有 www 但没有 https:// 的请求。主机名和路径保持不变。感谢 Nicolás Echániz 和 Joseph Scott 在http://joseph.randomnetworks.com/2004/07/22/redirect-to-ssl-using-apaches-htaccess/上的博客指出第二条规则。

于 2013-04-02T21:39:05.160 回答
1

首先清除浏览器缓存并重新启动浏览器。

Magento 设置更改

打开管理面板并访问System -> Configration -> Web面板并将Base URL(安全和不安全)设置为https://www.domain.tld/.

然后设置

Auto-redirect to Base URL= 没有

Use Secure URLs in Frontend= 是的

Use Secure URLs in Admin= 是的

保存您的设置,清除您的 Magento 缓存

Magento .htaccess 更改

将此代码放在.htaccessRewriteBase 行下方的基本 magento 目录中:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
于 2013-04-03T09:53:23.483 回答