Force HTTPS using .htaccess - apache

How can I force HTTPS on my website? I tried the solution from this answer, but for some reason I get redirected to the parent directory when I visit the index page by clicking on a link (it worked fine before adding those lines of code in .htaccess). Also, when I try to visit my website using HTTP, it lets me do it. What am I missing?

With Apache, you have several alternatives - including .htaccess.
Look here:
https://wiki.apache.org/httpd/RedirectSSL
Per the documentation, your best bet is to use a Redirect directive inside the non-secureVirtualHost:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
Redirect permanent /secure https://mysite.example.com/secure
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Here is an .htaccess example, from the same link:
Redirect permanent /login https://mysite.example.com/login
Finally, look here for additional troubleshooting tips (for example, forgetting 'NameVirtualHost *:443' to enable Named virtual hosting for port 443):
Why might Apache ignore a virtual host with a ServerName matching the requested URL?

Related

is it possible to change the url name?

I have one machine on which I am running an application and can be accessed through an URL: http://myapp0:port and on another machine (connected to another internet provider) I have one web page which is accessing the first url: http:myapp0:port. Is it possible to change this URL http://myapp0:port into the http://myapp1.
on httpd.conf I have the following:
<VirtualHost *:80>
ServerAdmin myapp1.ro
DocumentRoot "${SRVROOT}/htdocs"
ServerName myapp1.ro
ErrorLog "${SRVROOT}/logs/myapp1.ro.com-error.log"
CustomLog "${SRVROOT}/logs/myapp1.ro-access.log" common
RewriteEngine On
RewriteRule "http://myapp0:9999" "http://myapp1.ro"
Thank you
Please use the following configuration within your VHost, this will redirect all the request to this specific Vhost to http://myapp1.ro.
Redirect / http://myapp1.ro

Apache redirect to https without mod_rewrite not preserving URL

I know there are many similar questions to this, but none of the solutions worked for me.
I recently added SSL to my site with a Let's Encrypt certificate. I am now in the process of forcing all traffic to https, but I have 1 case that I cannot get to work properly.
If the user types in http://www.example.com/page/, it gets redirected to https://www.example.compage. By dropping the final / from the url base, it prevents users from accessing that page directly.
However, http://www.example.com/page, http://example.com/page/, http://example.com/page all work fine. This behavior is the same whether or not the http:// is included.
I am doing the redirect without mod_rewrite per the Apache documentation.
Here is my httpd.conf
<VirtualHost *:80>
ServerName http://example.com/
ServerAlias http://www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /path/cert.pem
SSLCertificateKeyFile /path/privkey.pem
</VirtualHost>
The appropriate SSLxx values are set in ssl.conf as well to point to my cert, key, and chain.
Any help is appreciated, as that /page/ is where users would log into my site, so it is critical and often bookmarked.
I found the answer here: https://superuser.com/questions/751562/ssl-redirect-in-virtualhost-causing-url-to-break
Basically, you have to redirect to https://www.example.com\/
Apparently Apache tries to strip the final slash if its not escaped for whatever reason.

Apache Virtualhost Redirect Not Working

I have two simple redirects set up in my virtualhost for a subdomain; one is working, one is not:
<VirtualHost *:80>
ServerName subdomain.site.com
Redirect / https://subdomain.site.com/subdirectory/login.php
</VirtualHost>
<VirtualHost x.x.x.x:443>
ServerName subdomain.site.com
Redirect / https://subdomain.site.com/subdirectory/login.php
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/subdomain.site.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/subdomain.site.com.key
ErrorLog logs/ssl_error_log
CustomLog logs/ssl_access_log common
</VirtualHost>
The first redirect is working. That is, if someone simply types in subdomain.site.com in their browser it redirects to https and to the correct subdirectory. The second redirect is not working. If someone types in https://subdomain.site.com it says "Firefox has detected that the server is redirecting the request for this address in a way that will never complete" and the browser URL becomes "subdomain.site.com/subdirectory/login.phpsubdirectory/login.phpsubdirectory/login.phpsubdirectory/login.php..." instead of redirecting to the correct https://subdomain.site.com/subdirectory/login.php page. Can anyone point me in the right direction?
Edit: I updated the above VirtualHosts file to the newer version and the problem has changed so I updated the problem description as well.
Alright, none of the answers above worked so I had to keep working on this. Ultimately I removed the redirect line from the :443 virtualhost section and added the following two lines to the same section to get this to work correctly:
RewriteEngine On
RewriteRule ^/$ https://subdomain.site.com/subdirectory/login.php [R=301,NC,L]
You have to add this line to top of file
NameVirtualHost x.x.x.x:443 or domaine name:443
check you apache version.
You have to add this line to top of file :
NameVirtualHost x.x.x.x
and
Listen 80
Listen 443
regards
if you are on ubuntu (I mean debian based linux distro)in your /etc/hosts you should define a line like below :
127.0.0.1 yourdomain
and then in make a new file for your new site configuration in :
/etc/apache2/sites-available/
and name it like your domain name .conf just to don't forget what is that conf file for.
then enable new conf with following command
a2ensite your_conf_name
then restart apache.
now your new site configuration is ready.
now look at following link :
http://httpd.apache.org/docs/2.2/bind.html
you have to mention that your apache should listen on multiple port
in your case 80 , 443
If you are using SSL you should change default-ssl.conf settings like
<VirtualHost *:443>
ServerName subdomain.site.com
ServerAlias subdomain.site.com
DocumentRoot /var/www/html/subdomain.site.com

Htaccess non sensible subdomains

While I was searching for a hosting I found one called co.gp (they have more), so this hosting was great and I discovered something on it that it allows you to put any fakes subdomain you want for example.
If your website it's example.org the hosting allows you to put test1.example.org or test2.example.org.
I searched alot with the help of htaccess but I couldn't make one Like I'm looking for. So my question here. How Can I make fakes subdomains in my website ?
Ps : I don't want subdomains to be redirect for example if someone type test.example.org/something I want it to be the same as example.org/something.
Htaccess files will not be of much help here, because they are located under the document root, and are therefore evaluated after apache has determined which virtualhost points to this specific document root.
Assuming your DNS entries are well-configured, You should instead modify directly the apache main configuration.
If every subdomain points to a different document root, you will have to create a virtual host for each one:
<VirtualHost *>
ServerName example.org
DocumentRoot /path/to/example.org
# ...
</VirtualHost>
<VirtualHost *>
ServerName test1.example.org
DocumentRoot /path/to/test1.example.org
# ...
</VirtualHost>
If, instead, several subdomains point to a single document root, you can use the ServerAlias directive:
<VirtualHost *>
ServerName example.org
ServerAlias test1.example.org
DocumentRoot /path/to/example.org
# ...
</VirtualHost>
If all of the possible subdomains point to the same document root, you can relieve yourself from the pain of listing every subdomain in the ServerAlias list, and use a wildcard instead:
<VirtualHost *>
ServerName example.org
ServerAlias *.example.org
DocumentRoot /path/to/example.org
# ...
</VirtualHost>
There are of course more configurations directives needed to ensure each virtual host works properly, but these are the main building blocks you should understand to have a working setup.

Apache Multiple VirtualDocumentRoot

Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?
This is naming convention I am currently using and would like to continue to use:
host directory
127.0.0.1 domain domain.com
127.0.0.1 sub.domain domain.com_sub
Then in my vhosts section of the httpd.conf I have:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>
The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.
Apache typically will pick the first virtual host whose ServerName or ServerAlias matches the host name provided in the Host HTTP header. In your case, since you have no ServerName directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName and ServerAlias to make the configuration explicit. Try something more like this:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName domain.com
ServerAlias www.domain.com
VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>
That should use /var/www/domain.com for http://domain.com and /var/www/www.domain.com for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com for http://sub.domain.com, /var/www/blah.domain.com for http://blah.domain.com, and so on.
You have to qualify the backreferences when you want to put a '.' in the file path. So you need to have it like this:
VirtualDocumentRoot /var/www/%2.0.%3_%1
Regarding the OP and the issue with "/var/www/html" being set:
The problem I had to this was using %1 instead of %2. Here's my working example:
ServerAlias www.*.org.au
UseCanonicalName Off
VirtualDocumentRoot /path/to/sites/%2/pub
Hope that helps someone!
I read the docs on "Directory Name Interpolation" in mod_vhost_alias docs.
I finally found a configuration that allows flexible subdomain creation.
See apache docs on mod_vhost_alias
If your root dev domain has 3 parts like dev.example.com you can use %-4+ as a placeholder for everything before the root domain. If it has 4 parts, use %-5+.
<VirtualHost *:80>
VirtualDocumentRoot "/var/www/%-4+/webroot"
ServerName www.dev.example.com
ServerAlias *.dev.example.com
php_admin_value auto_prepend_file /var/www/setdocroot.php
</VirtualHost>
This way you can create a directory named /var/www/sub.domain/webroot and access it with the url sub.domain.dev.example.com.
The line php_admin_value auto_prepend_file /var/www/setdocroot.php fixes the docroot on some systems like OSX 10.9+
Here is the content of setdocroot.php :
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
?>
What I am noticing with this configuration is that $_SERVER['DOCUMENT_ROOT'] is pointing to /var/www/html and not to the vhost's doc root.
weird.
Update (2010-07-24):
I just wrote a blog post how to setup your http proof server
http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html