Apache Virtualhost Redirect Not Working - apache

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

Related

Mutliple domains in the same httpd.conf

I have two domain names that point to the same IP address. One of the domains is hosted locally on the server, the other is just a forward to another domain (we migrated our Confluence to the cloud, but need to keep old links alive)
My httpd.conf looks a little like this (a lot of, hopefully, irrelevant info removed).
Listen 80
Listen 443
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerName esd.domain.com
Redirect / https://esd.domain.com/
</VirtualHost>
<VirtualHost *:443>
ServerName esd.domain.com
DocumentRoot "C:/Apache/htdocs"
SSLEngine On
SSLCertificateFile "/Apache/conf/esd.domain.com.crt"
SSLCertificateKeyFile "/Apache/conf/esd.domain.com.key2"
</VirtualHost>
<VirtualHost *:80>
ServerName documentation.domain.com
Redirect "/display/" "https://domain.atlassian.net/wiki/display/"
Redirect "/" "https://domain.atlassian.net/"
</VirtualHost>
<VirtualHost *:443>
ServerName documentation.domain.com
Redirect "/display/" "https://domain.atlassian.net/wiki/display/"
Redirect "/" "https://domain.atlassian.net/"
</VirtualHost>
This works great for esd.domain.com, but documentation.domain.com ends up at esd.domain.com as well.
Does anyone have any pointers on how I should be doing this please? I'm an httpd novice who has inherited this system and struggling! Thanks in advance.
This page seems to suggest it's possible: https://docs.jelastic.com/name-based-apache-virtual-host
The only difference with mine is that the second domain is a redirect.
I found out that the issue wasn't with the .conf file, rather that
httpd -k restart
Wasn't actually restarting due to a permissions problem. It was reporting errors in the .conf file so gave the impression it was restarting, when in fact it wasn't able to shut down the running instance. In the end I found a small error in the log file that the service couldn't be stopped and sorted the permissions out.
Now the above .conf file works a charm.

Force HTTPS using .htaccess

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?

VirtualHost setup always doesn't work

I'm trying to set up a virtualHost for mampstack (NOT MAMP). This is what I've done so far:
In my httpd.conf file I've checked
Listen 8080
This is correct (I'm listening to the port 8080, NOT 80).
Then I've uncommented: Include conf/extra/httpd-vhosts.conf in my httpd.conf file
In my hosts file I have added the following: 127.0.0.1 mext-pst.local.
In httpd-vhosts.conf I've added:
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs"
ServerName 127.0.0.1
ServerAlias localhost
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN localhost
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/mext-pst-dashboard/web"
ServerName mext-pst.local
ServerAlias mext-pst.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN mext-pst.local
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
</VirtualHost>
Now when I go to http://mext-pst.local/ I just get an error of my browser that he can't connect with the page ... .
When I go to http://mext-pst.local:8080/ I get the following error:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: DNS lookup failure for: mext-pst.local:8080
When I go to http://mext-pst.local:8080/index.php it works ...
Change 8080 to 80 its the default. But if you want your site to run on 8080, then you have to use it. Another solution might be to rewrite the url, that is when your server gets the url, it rewrites it with port number (8080).
First of all change Listen 8080 to Listen 80, as you want your application to be accessible only with http.
In your http-vhost.conf file put following lines (of course after removing previous changes). In the following configuration yourDefaultHttpFolder means the default http folder. You might have changed it. So correct it depending on your system.
<VirtualHost *:80>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/yourDefaultHttpFolder"
ServerName 127.0.0.1
ServerAlias localhost
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN localhost
<Directory /Applications/mampstack-5.4.20-0/apache2/htdocs/yourDefaultHttpFolder>
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/mext-pst-dashboard/web"
ServerName mext-pst.local
ServerAlias mext-pst.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN mext-pst.local
</VirtualHost>
This configuration is working on my server, when ever I try to access using 80 it rewrites the URL to my 8080 port and I see the content of that folder, not the the default index page.
You've got to change the port to *:80 and also if you're going to use a different name then the servername make sure to take up NameVirtualHost *:80 in your httpd.conf.
Since you are not listening on port 80, getting an error when you go to the URL without a port seems to be the correct result, right?
And when you go to port 8080 you are getting a Proxy Error. Are you sure there is no other software running on port 8080 or your browser doesn't have a proxy entered? Apache would not be giving a Proxy Error. I suspect this error is coming from somewhere else.
About your configuration, I am not exactly sure what it is that you are trying to achieve but, if you are trying to get some documents served when you go to localhost:8080 and another set of documents served when you go to mext-pst.local:8080 than you are almost there, NameVirtualHost *:8080 is correct and needs to be there, remove redirect lines as you don't need them (unless my assumption is wrong).

Simplest way to get MediaWiki to require HTTPS on all pages?

I need a MediaWiki installation to require the use of https (and reject normal http). I've spent 2 hours looking. Setting $wgServer doesn't work and closing port 80 in httpd.conf doesn't work either.
My wiki installation is run on an Apache server.
I've just done this on Ubuntu 14 (for the first time today, so there may be a better way!) by setting
$wgServer = "//myhostname.com/mediawiki";
This makes the server name "protocol relative" so it works with either HTTP or HTTPS. You can probably just set it to https://... though.
Then configure apache2 to redirect all HTTP traffic to HTTPS:
Edit the default SSL configuration (this assumes you are just using the default site):
sudo vim /etc/apache2/sites-available/default-ssl.conf
to read something like:
# Redirect HTTP to HTTPS
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
# Normal HTTPS config for default site
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
ServerAdmin admin#example.com
ServerName example.com
DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the default SSL site, if you haven't already (this creates a link from sites-enabled to sites-available)
sudo a2ensite default-ssl
This assumes that you have already obtained an SSL certificate (I generated a self-signed one) which has been placed in /etc/apache2/ssl/apache.pem and /etc/apache2/ssl/apache.key as referenced in the config above.
Finally get apache to use the new config:
sudo service apache2 restart
(Or reload may be enough)
My answer assumes that you already have Apache listening for https traffic on port 443. If that's not the case, you need to set that up first. The procedure will be different depending on what operating system you are running.
You want to do this in Apache. On my Ubuntu system, there's a file /etc/apache2/ports.conf which contains the following line:
Listen 80
You will have a similar config file that contains that line. Delete it, and don't forget to restart Apache.
Another way to accomplish this, which allows for more complex Apache configurations where you allow HTTP access to some parts of the site, is to use a .htaccess file in your MediaWiki directory. Try this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Given that your web server is set up to support https in general, insert or update the following line in your LocalSettings.php configuration file of MediaWiki:
$wgForceHTTPS = true;
This redirects all queries using http to https and is an alternative to a redirect rule in the web-server configuration.
See also:
$wgForceHTTPS
MediaWiki HTTPS Manual

add a server alias to the domain's VHOST?

I have a VPS. I hosted a domain ipointing to a sub directory of the www folder. The domain works fine till the home page. The moment I start going to other pages its shows my servers [orginalname]/[subdirectory name] . I think I need to add a server alias to the domain's VHOST. Can anyone tell me how to do that??
Are you using Apache?
Try with
<VirtualHost *:80>
DocumentRoot "/path/to/document/root"
ServerName name1
ServerAlias name2
...
I did it using the proxy Apache option, this is it:
My VirtualHost is http://dlx/ and I want to add an "alias" like http://dlx/drupal/
In the httpd.config file I added a proxy configuration:
<VirtualHost 127.0.0.1>
ServerName dlx
DocumentRoot "C:/deluxe/"
<LocationMatch /drupal/>
ProxyPass http://localhost/drupal/
ProxyPassReverse http://localhost/drupal/
</LocationMatch>
</VirtualHost>
Configure .htaccess on my dlx virtualhost (C:/deluxe/):
RewriteRule ^drupal/(.*)$ http://localhost/drupal/$1 [P,L]
That's it. It works for me, I hope it also works for you.