I installed SSL and now I want to redirect my domain name to HTTPS . my .htaccess file has this configuration provided by my CMS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Options +FollowSymLinks
Options -Indexes
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]`
What CMS are you using? Most common CMS have plugins which will do a better job updating any non-HTTPS resources to HTTPS.
I know you requested no referral to another link but:
The Apache docs recommend against using a rewrite:
To redirect http URLs to https, do the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
This snippet should go into main server configuration file, not into .htaccess as asked in the question.
This article might have come up only after the question was asked and answered, but seems to be the current way to go.
https://stackoverflow.com/a/21798882/11039985
If you must use htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Update: per your response, after making a backup of the htaccess file and try:
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]
I think what you are looking for is setting up hsts. You can find more about this header here. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
This link will help in setting up hsts. https://www.globalsign.com/en/blog/what-is-hsts-and-how-do-i-use-it/
Related
I am unable to direct my example.in to https://www.example.in
I am using .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if
not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
AcceptPathInfo On
I have found this code on stackoverflow but it's not working in my project
my site is on AWS and using virtual host of apache2 php 7.0
It is not forcing www and and i have tried other code .htaccess but if i manually delete the www from url it is not redirected to www .
Thanks guys for support ,
I have solved my problem it was because ssl was install on my site and hence in /etc/apache2/sites-available/
I found default-ssl.conf file which contains some of setting which was, i guess not allowing me ovveride with .htaccess .
but now after adding
<Directory /var/www/myfolder > AllowOverride All </Directory>
The htaccess is working fine.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
My app is hosted on heroku, and I am using Cakephp API. I want it to be redirect to https://www. and enforcing the www. subdomain.
And, they have two domains point to the same app.
I have the follow code, in the /app/.htaccess
RewriteEngine on
RewriteBase /
#apply if no https
RewriteCond %{HTTPS} off
#Ignore when is local env or any staging env
RewriteCond %{HTTP_HOST} !^local(.*) [NC]
RewriteCond %{HTTP_HOST} !^(.*)heroku(.*) [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
#apply if https
RewriteCond %{HTTPS} on
#Ignore when is local env or any staging env or subdoamin is www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^local(.*) [NC]
RewriteCond %{HTTP_HOST} !^(.*)heroku(.*) [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
#default from cakephp
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
I have the follow code, in the /app/webroot/.htaccess
RewriteEngine On
RewriteBase /
#redirect any request from .poa.br to .com.br
RewriteCond %{HTTP_HOST} ^(.*)example\.poa\.br$ [NC]
RewriteRule ^(.*)$ https://www.example.com.br/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
But it doesn't work. Just the follows requests are redirect to https://www.example.com.br:
https://www.example.com.br
https://www.example.poa.br
http://www.example.poa.br
This option go to domain, but without https prefix.
http://www.example.com.br
The other options doesn't work (return DNS_PROBE_FINISHED_NXDOMAIN):
https://example.com.br
http://example.com.br
https://example.poa.br
http://example.poa.br
In cakephp3 I struggled with this. My solution was to change the first .htaccess file from
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
TO
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Notice the 302, the reason I struggled is that the .htaccess got cached and made testing very difficult, I even went as far as delete all the cakephp code in the index.php in the webroot and just echo out 'test' Once I got it working I changed the 302 to 301 so that the .htaccess can hard cache in the browser. ( To test if values like HTTP:X-Forwarded-Proto exist I var_dump($_SERVER) in the index.php file )
IF you are on a server without HTTP:X-Forwarded-Proto
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Hello make sure rewrite module is on before this works.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R,L]
and in your base url you need to add (s) to http.
I hope this helps you.
You can also add 'www'
just add this
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
We cannot redirect domain to www.domain in Heroku using .htaccess. We need configure it using Heroku command. See the articles:
https://github.com/Helabs/pah/wiki/Configuring-domain-on-Heroku
https://devcenter.heroku.com/articles/custom-domains#add-a-custom-root-domain
I'm looking how to force https.
Change in your cakephp project folder your htaccess file like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)http(\:|\%3A)(.*)$
ReWriteRule .* - [F]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
And be sur that your server (or your virtual host) listen to the port 443 (for https)
<VirtualHost *:80>
ServerAdmin subdomain#example.br
ServerName subdomain.example.br
ServerAlias subdomain.example.br
DocumentRoot /var/www/subdomain
<Directory /var/www/subdomain/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ServerSignature Off
ErrorLog /var/log/apache2/error.log
</VirtualHost>
<VirtualHost *:443>
ServerAdmin subdomain#example.br
ServerName subdomain.example.br
ServerAlias subdomain.example.br
DocumentRoot /var/www/subdomain
<Directory /var/www/subdomain/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM
SSLCertificateFile /[path to your CRT file]
SSLCertificateKeyFile /[path to your PEM file]
SSLCertificateChainFile [path to your CRT file]
SSLCACertificatePath /etc/ssl/certs/
SSLCACertificateFile /[path to your CRT file]
ErrorLog /var/log/apache2/error.log
</VirtualHost>
I made all configurations in /app/webroot/.htaccess
There is a trick to identify if request to Heroku is https.
Follow the code:
RewriteEngine On
RewriteBase /
#Identify if it is https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
#ignore local env
RewriteCond %{HTTP_HOST} !^local(.*) [NC]
#apply redirect in all subdomain
RewriteCond %{HTTP_HOST} ^(.*)example\.com\.br$ [NC]
RewriteRule ^(.*)$ https://example.com.br/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
We decided to redirect all request to naked domain, but if you want to change to www you coult implement the follow command.
RewriteRule ^(.*)$ https://www.example.com.br/$1 [NE,R=301,L]
Thanks
i already have .htaccess file in root directory with this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ list.php?user=$1 [L]
Now, i also want to ridirect all HTTP traffic to HTTPS pages using this htaccess file without removing existing codes, i tried some codes but i see error by browsers such as "Not Ridirecting Properly".
plz tell me some code according to the existing .htaccess file.
Thanks A Lot
If you need to do it with .htaccess, do it like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You can also do it easily with a separate VirtualHost:
<VirtualHost *:80>
ServerName yourname.example.com
RewriteEngine On
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
ServerName yourname.example.com
I would like that the .htaccess don't add the SSL on the home link
http://domain.com
but on every other links in the website
https://domain.com/folder/index.html
So only the home page would not be secure. What the best .htaccess config for that ?
Thanks
Use RewriteRule to match that anything is present beyond the / and if matched, rewrite to SSL:
RewriteEngine On
# If ssl is not already active
RewriteCond %{HTTPS} !=on
# .+ matches one or more of any character... An empty string would not match
RewriteRule ^(.+)$ https://%{HTTP_HOST]}%{REQUEST_URI} [L,R=301]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^.+$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} =on
RewriteRule ^$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
There is a problem with my .htaccess file. If I type in "website.com" it redirects me correctly to "www.website.com", but if I type in "website.com/level1/level2" it redirects me to "www.website.com/index.php/level2" and gives me a 404 error.
Here is what I have in my .htacces file:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8
#Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
Any suggestion how to solve the issue?
Thank you.
I think what's happening is that you are rewriting to index.php before you are doing the external redirect. If you switch the order of your RewriteRules, it should fix the problem. Try this:
RewriteEngine on
#Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8
This way, the external redirect to www is done first, and when the request comes back, the internal index.php is then applied, instead of the other way around.
If you have access to the server config, it is better to do hostname redirects with plain old Redirect which is designed for this exact situation. Don't resort to the complexity of mod_rewrite until you really need to.
Remove the ServerAlias for example.com in the <VirtualHost> for your main web site, and add it as a separate virtualhost instead:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
Always put rules that cause an external redirect before those rules that just cause an internal redirect. Otherwise an already internally rewritten URL would be redirected externally using the internal URL.
So in your case swap the order of your two rules putting the rule with the R flag before the other rule:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php