I'm trying to redirect all my foo.mydomain.com requests to https://foo.mydomain.com.
My VirtualHost contains this:
<VirtualHost *:443>
ServerName foo.mydomain.com
# Indexes + root dir
DirectoryIndex index.html
DocumentRoot /home/web/foo.mydomain.com/htdocs
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
SSLVerifyClient none
<IfModule mime.c>
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
</IfModule>
# Logfiles
ErrorLog /home/web/foo.mydomain.com/logs/error.log
CustomLog /home/web/foo.mydomain.com/logs/access.log combined
</VirtualHost>
So now I'm trying to redirect all the http-request to https with mod_rewrite in this .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://foo.mydomain.com
Does anyone knows what I'm doing wrong? Thank you in advance!
Try the following in virtual host config for the non https site.
I.e. my settings to do this are:
<VirtualHost *:80>
RewriteCond %{SEVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
.....
</VirtualHost>
This should redirect all incoming http request to https. Then in the config for port 443 you can do whatever you need....
Note: should work in htaccess as well.
Hope that helps. Not sure what you meant by 'default page' but in title and too low rep to leave comments...
Let me know...
Cheers
Robin
Related
I have an issue redirecting from the http site to the https site.
This index.php file is in the root folder, redirecting to a suburl:
<?php
header("Location: /news/");
?>
the url https://www.example.com is correctly redirecting to https://www.example.com/news/
the url https://www.example.com/index.php is correctly redirecting to https://www.example.com/news/
the url http://www.example.com/index.php is correctly redirecting to https://www.example.com/news/
however the url http://www.example.com is showing an empty directory, as in the image below:
these are the config files:
/etc/apache2/sites-available/www.example.com.conf
<VirtualHost *:*>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example.com/httpdocs
ServerAlias example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
/etc/apache2/sites-available/www.example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example.com/httpdocs
ServerAlias example.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
How can I correctly redirect from http://www.example.com to https://www.example.com/news/ ?
One has two options -- Virtual Host Redirection, and Request Rewriting. Adapt the following configuration fragments to your needs.
Virtual Host Redirection
To be prefered as being simpler and safer. Redirect an HTTP virtual host (Port 80) to a HTTPS virtual host (Port 443) via the virtual hosts configuration:
# Listening for HTTP connections
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
# permanently redirects to the site's HTTPS version
Redirect permanent / https://www.example.com/
</VirtualHost>
# Listening for HTTPS connections
<VirtualHost _default_:443>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# Further configurations...
</VirtualHost>
Request Rewriting
Alternatively -- usually, when the virtual hosts config is not accessible -- one can edit the .htaccess file (create it, if necessary) to rewrite HTTP to HTTPS requests. For that the module mod_rewrite has to be enabled, so
LoadModule rewrite_module modules/mod_rewrite.so
in httpd.conf has to be set (and usually is, by default). Then add to the .htaccess file:
# Enable rewriting
RewriteEngine On
# Check for HTTPS, if no, execute next line
RewriteCond %{HTTPS} off
# Redirect to HTTPS with status code 301 (moved permanently)
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
I used:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualHost>
Type the code into the conf file.
Using Ubuntu 16.04 and Apache virtual hosts.
I have a Wordpress site: www.example.com
Other sites commonly link to URLs such as:
sub1.example.com/page1
sub2.example.com/page2
I want to redirect them to:
example.com/page1
example.com/page2
I have tried:
RewriteCond %{HTTP_HOST} ^sub1.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
and
RewriteCond %{HTTP_HOST} ^sub1\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
With both of these configurations, I get the following behaviour:
sub1.example.com goes to example.com as intended
sub1.example.com/page1 goes to a generic Apache 404 page "The requested URL was not found on this server." with the URL sub1.example.com/page1, it is not redirecting
My DNS setup involves A records pointing at the server IP for example.com, www, sub1 etc.
My virtual host .conf file looks like this:
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /var/www/example.com/public_html
ServerName example.com
ServerAlias www.example.com
<Directory /var/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
</VirtualHost>
I have tried adding sub1.example.com as a ServerAlias and reloading Apache to no avail.
How might I solve this?
I fixed the issue by creating individual virtual hosts for each subdomain, each with its own files structure and .htaccess file.
I have installed SSL Certificates on my website and on the example.com everything works fine, meaning that typing example.com redirects correctly to https://example.com. However, I have installed a certificate for a subdomain as well such that the link becomes: subdomain.example.com.
My goal is to have subdomain.example.com redirect to https://subdomain.example.com . This might sound weird but this semi-works meaning that when I first surf to subdomain.example.com it uses the http protocol but when I refresh that same page it switches to https protocol.
This is my VirtualHost conf file (port 80):
<VirtualHost *:80>
ServerName subdomain.example.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
ProxyPassReverse http://127.0.0.1:8181
ProxyPassReverse http://example.com/
</Location>
RewriteEngine on
#Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
RewriteCond %{SERVER_NAME} =subdomain.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/ [R=301,L]
</VirtualHost>
I have removed to non related lines from this sample above. Here is the 443 conf file:
< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
#Allow forwarding to gitlab-workhorse
ProxyPassReverse http://127.0.0.1:8181
ProxyPassReverse http://domain/
</Location>
RewriteEngine on
#Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/subexample.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
SSLUseStapling on
Header always set Content-Security-Policy upgrade-insecure-requests
</VirtualHost>
</IfModule>
Worth noting is that I am using certbot.
Hopefully someone can help me.
You say "My goal is to have subdomain.example.com redirect to https://subdomain.example.com".
Then why have all that proxy configuration in your :80 VirtualHost? Simply force the redirection to :443, and let :443 handle the proxy (and other).
So your VirtualHost would become:
<VirtualHost *:80>
ServerName subdomain.example.com
CustomLog logs/subdomain_80_access.log combined
ErrorLog logs/subdomain_80_error.log
RewriteEngine On
RedirectMatch ^/(.*)$ https://subdomain.example.com/$1
</VirtualHost>
My site works fine on http but using https with a valid certificate will only load the landing page successfully. Trying to hit any other page results in URL not found.
I have a clone of the site on the same server with a different site name (test.mysite.com) working with a self-signed certificate on https. Not sure why one works and the other doesn't.
Apache conf files the are same (except site names).
Is it something with the cert file? I set up the self signed one but someone else set up the paid one.
Where do I look?
/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
php_value max_input_vars 10000
php_value suhosin.get.max_vars 10000
php_value suhosin.post.max_vars 10000
php_value suhosin.request.max_vars 10000
~
site.conf
VirtualHost *:80>
ServerAdmin webmaster#mysite.ca
ServerName registration.mysite.ca
# Indexes + Directory Root.
DirectoryIndex index.php index.html
DocumentRoot /var/www/html/registration.mysite.ca/public
# SSL certs
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/registration.mysite.ca.crt
SSLCertificateKeyFile /etc/apache2/ssl/registration.mysite.ca.key
SSLCACertificateFile /etc/apache2/ssl/gd_bundle-g2-g1.crt
<IfModule mod_security2.c>
SecRequestBodyNoFilesLimit 5242880
</IfModule>
<Directory /var/www/html/registration.mysite.ca/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
SSLEngine on in <VirtualHost *:80> is pretty much useless because port 80 will never serve SSL content, so your certificate is definitely not used here.
You must have another <VirtualHost *:443> somewhere, potentially with a different DocumentRoot.
When you load css/js/images are you using src="http://registration.mysite.ca" ? If so change it to src="//registration.mysite.ca" and it should work.
As this is tagged Laravel, I suggest using Laravel Forge to take care of everything like this. Big help for me. As for the question at hand, ensure that the virtual host is set to 443 and then force redirect all traffic to https.
Just add this into apache site config and change the domain. (note HTTPS)
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
I am using this code in httpd.conf
<VirtualHost *:4080>
# This will enable the Rewrite capabilities
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
ServerAdmin serveradmin
DocumentRoot "/etc/zpanel/panel/"
ServerName panel.mydomain.com
ServerAlias *.panel.mydomain.com
AddType application/x-httpd-php .php
<Directory "/etc/zpanel/panel/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I need http://domain.com:4080 to redirect to secure ://domain.com:4080, which is happening but certificate is not working
If I remove the auto redirection code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) [https://%{HTTP_HOST}%{REQUEST_URI}][1]
and goto secure: //domain.com:4080 in browser myself certificate shows up.
I need something like cpanels auto redirection to https
Try http://ziing.net:2087/