问题
I'm trying to redirect magento from any.example
to https://www.example.com
htaccess code:
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
############################################
## 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}]
# // REDIRECT MY DOMAIN HTTPS and add www //
RewriteCond %{HTTP_HOST} !^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https : //www.example.com%{REQUEST_URI} [R=301,L,NE]
############################################
## 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>
回答1:
The following rewrite rule should forward all non-https requests to the corresponding page via https with a 301 redirect.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
Also refer below link
https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
回答2:
Make sure your theme does not have references to http
then in your htaccess file add/replace this,
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
############################################
## REDIRECT TO HTTPS ALWAYS
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
回答3:
If I'm understanding your goal correctly, then it seems like the negate of your first Cond pattern is incorrect (your negative lookahead already excludes the www). Try this instead:
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
I would also tidy up the rule itself:
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
UPDATE with all corrections:
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
回答4:
This solution is working great...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
来源:https://stackoverflow.com/questions/35122725/i-cant-redirect-magento-domain-to-https-and-add-www