I have a little problem and I don't know why is that.
I tried :
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
and
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
But no succes.
It redirect the website from : http://domain.com to http://www.domain.com
but not from http://domain.com/sample-page to http://www.domain.com/sample-page
Why?!
Update : I use HHVM 3.6.6 . This can be the reason?! And place for .htaccess is in httpd-app.conf (Bitnami HHVM)
Bitnami developer here.
If you want to apply this redirection by default, you can add it in the default VirtualHost in the "installdir/apache2/conf/bitnami/bitnami.conf" file.
<VirtualHost *:80>
ServerName app.example.com
ServerAlias www.app.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
...
<VirtualHost *:443>
ServerName app.example.com
ServerAlias www.app.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
...
Please note that you will need to restart Apache to apply the changes.
installdir/ctlscript.sh restart
You can learn more about how to configure Apache using this link.
https://wiki.bitnami.com/Components/Apache
I hope it helps.
Jota
Related
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 have this configuration in virtualhost for redirect https non www to https www but this configuration is not working
So when I visit https://myweb.com/ it not redirect to https://www.myweb.com/
Listen 443
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.myweb.com/$1 [R=301,L]
.... so on ....
</VirtualHost>
Is something wrong with my configuration?
Try and use this rule instead:
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.myweb.com%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your cache before testing this.
I'm trying to force non-www + https in .htaccess on an AWS EC2 instance.
While there is an abundance of apparently working solutions right here on StackOverflow, all of those only produce redirect loops for me.
I get a redirect loop when I try this rule:
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
#RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
(via Force non-www and https via htaccess)
Same for this one:
RewriteEngine on
#RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
#RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
Both seem to work for the respective OP, and as indicated by some of the comments, they work for others too.
I have the following VirtualHosts set up in my httpd.conf:
NameVirtualHost *:80
Listen 8443
NameVirtualHost *:8443
<VirtualHost *:80 *:8443>
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/domain.com
ServerName domain.com
ServerAlias *.domain.com
ErrorLog logs/domain.com-error_log
CustomLog logs/domain.com-access_log common
</VirtualHost>
For context: The :8443 port receives traffic from an AWS ELB (load balancer) which routes :443 SSL requests to this particular port, because the SSL certificate is installed on the load balancer itself.
What could be the issue for the redirect loops?
Behind the load balancer you have to handle things differently. You won't be checking for https in the normal way because of the SSL Offloading with your LB. You would need to check the X-Forwarded-Proto
Try these rules and see how they work.
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
RewriteRule ^.*$ https://%2%{REQUEST_URI} [R=301,L]
To avoid redirect loop you can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
Make sure to clear your browser cache before testing this.
I'm trying to get all these conditions to redirect to https://www.domain.com/admin/ but only for a certain directory
RewriteCond
http://domain.com/admin/
http://www.domain.com/admin/
https://domain.com/admin/
RewriteRule
https://www.domain.com/admin/
I tried this but agave me a redirect loop:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/admin [R=301,L]
Thank you
UPDATE
I have a feeling that the vhost on the server has something to do with it. Here is what I have in /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
ServerAdmin webmaster#domain.com
ServerName default.domain.com
# Set application environment.
SetEnvIf Host "^" APPLICATION_ENV=production
SetEnvIfNoCase Host "^(stage\.)(.*)$" APPLICATION_ENV=staging BOGUS_HTTP_HOST=$2
RewriteEngine On
#RewriteLogLevel 3
# Remove parts from the front if host name has too many parts,
# unless a directory with the right name exists in /var/www.
RewriteCond %{HTTP_HOST} "^([^.]+)\.(.*[^.]+\.[^.]+)$"
RewriteCond /var/www/%{HTTP_HOST} !-d
RewriteRule "^(.*)$" "http://%2$1" [R,L]
# Redirect admin and member requests to SSL connection
RewriteCond %{REQUEST_URI} ^/(admin|member|join|order)(/.*)?$ [NC]
RewriteRule ^.*$ https://%{HTTP_HOST}/%1%2 [R,L]
# Set the bogus ssl header
SetEnv BOGUS_HTTPS "off"
UseCanonicalName Off
VirtualDocumentRoot /var/www/%0
</VirtualHost>
Have this rule in /admin/.htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
I just wanted to redirect all users to access the site with the 'www.' prefix (http://example.com/... will be redirected to http://www.example.com/...)
I have added the default code in drupal 7 .htaccess.
It was working perfectly in Mozilla and Chrome and not in IE.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Please advice this to work in IE
Try changing your .htaccess lines to
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You can also do it in your apache setup. I would not do it in .htaccess file.
In your
/etc/apache2/sites-available/SITENAME
do something like:
<VirtualHost *:80>
ServerName http://www.example.com
Redirect / http://example.com
</VirtualHost>