Apache2 redirect with wildcard subdomain - apache

I placed the following config in apache site conf file:
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
ServerAdmin webmaster#localhost
Redirect permanent / https://domain.com/
</VirtualHost>
How can I have subdomains redirect without going to the main domain?
If I type http://subdomain.domain.com/ it redirects to https://domain.com/ but I want it to redirect to https://subdomain.domain.com/. I also want it to be a wildcard so that it works for all subdomains.
Is it possible without engaging the mod_rewrite module?

I don't think it's possible without mod_rewrite, but I'm not sure. With rewrite is very simple though:
RewriteEngine On
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

Match URL to a specific file in Apache

I have a domain which is currently redirecting to another page. This is my virtual hosts file:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / http://www.anotherdomain.com
</VirtualHost>
Now I need an SSL certificate for that page, and to verify I must provide a file under a certain URL.
Is it possible to provide the apache configuration that matches e.g. example.com/verify_file_123.txt to a file on my filesystem, e.g. /var/www/my_site/verify_file_123.txt while still having redirects for all other routes?
I tried it with
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias "/verify_file_123.txt" "/var/www/my_site/verify_file_123.txt"
Redirect permanent / http://www.anotherdomain.com
</VirtualHost>
but that doesn't work.
Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied.
You can try something like this:
Alias "/verify_file_123.txt" "/var/www/my_site/verify_file_123.txt"
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/verify_file_123.txt
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=301,L]

Redirect from non-www to www with SSL

I have an Apache server hosting my HTML site. I have it setup currently to be SSL-enabled on www.mysite.com. It will also redirect from http://example.com to https://www.example.com.
However, I am having two issues:
First, I cannot figure out how to redirect:
http://www.example.com to https://www.example.com (not the ssl there)
Second, the www redirect also doesn't occur I navigate to:
https://example.com/
Below is the relevant portions of my httpd.conf file.
<VirtualHost *:80>
ServerName default
ServerAlias *
<IfModule mod_rewrite.c>
RewriteEngine on
# WITH 'www.'
RewriteCond %{HTTP_HOST} !^www.(.*) [nocase]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [redirect=permanent,nocase,last]
</IfModule>
</VirtualHost>
<VirtualHost *:443>
ServerName www.exampe.com
SSLENGINE on
SSLCertificateFile /etc/pki/tls/signed/my.crt
SSLCertificatekeyFile /etc/pki/tls/signed/my.key.csr
SSLCertificateChainFile /etc/pki/tls/signed/my.ca-bundle
</VirtualHost>
Does anyone have any idea how to modify the above VirtualHost elements to include the https redirect (bullet point 1) and have the redirect enabled for the ssl-enabled, non-www form of the url (bullet point 2)?
For first case remove the IfModule and mod_rewrite mess and add this in the non-SSL virtualhost:
Redirect / https://www.example.com/
For Second case add this in the SSL VirtualHost:
UseCanonicalName on

Redirect in Apache while preserving subdomain

I have a website that is heavily made up of subdomains. I want to redirect all subdomains to their HTTPS counterpart but I'm not sure how to do that in my vhost
http://subdomain1.example.com/ -> https://subdomain1.example.com/
http://subdomain2.example.com/ -> https://subdomain2.example.com/
...
http://example.com/ -> https://example.com/
Adding the following code should solve your issue.
<VirtualHost *:80>
ServerName subdomain1.example.com
...
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName subdomain1.example.com
...
</VirtualHost>
Note: check that mod_rewrite is enabled

How to redirect a web page while not changing the URL (using Apache VirtualHost)

I'm setting up Virtual Hosts in my Apache web server and already have rules in place that just do a simple 301 redirect from one URL to another. However, I've now been asked if I can write a rule that redirects to another page while keeping the URL the same and I've tried this:
<VirtualHost *:80>
ServerName ZZZ.com
ServerAlias YYY.com ZZZ.com
Redirect / YYY.com
</VirtualHost>
And this:
<VirtualHost *:80>
ServerName ZZZ.com
RewriteEngine on
RewriteRule ^/(.*) YYY.com/$1 [R]
</VirtualHost>
Neither did what I expected of them. They look wrong to me but I'm just not finding any helpful information anywhere. I've looked at http://httpd.apache.org/docs/2.4/vhosts/examples.html and http://httpd.apache.org/docs/current/mod/mod_rewrite.html - Neither of them were very helpful.
<VirtualHost *:80>
ServerName YYY.com
ServerAlias ZZZ.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^yyy\.com$ [NC]
RewriteRule (.*) YYY.com$1 [R=302,L]
</VirtualHost>
You were missing a condition to prevent it from redirecting correct urls.

Redirecting http://example.com to http:/www.example.com while keeping http://subdomain.example.com access intact

I have a website on which I've enabled subdomain access such as:
http://subdomain1.example.com
which accesses the same code, but passing a domain parameter in order to show a different microsite. The httpd.conf code for that looks like this:
RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteRule forums.html$ /browse.php?type=forums&domain=%1 [QSA]
Now I need to redirect http://example.com to http://www.example.com
I tried this, but it did not work:
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
(source: http://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/ )
EDIT1
<VirtualHost IPADDRESS:80>
ServerAlias *.example.com
DocumentRoot /var/www/html/abc
ServerName www.example.com
UseCanonicalName On
EDIT2
Hi mreithub,
The setup I need is something like this:
http://X1.example.com should use the code in /something/X1
http://X2.example.com should use the code in /something/X2
http://example.com should redirect to http://www.example.com
http://www.example.com/scriptA.php should use the code in /var/www/html/abc/scriptA.php
http://whateverelse.example.com/scriptA.php should use the code in /var/www/html/abc/scriptA.php but be passed with a 'domain=whateverelse' parameter (but the URL on screen should show always show the domain as being http://whateverelse.example.com )
I had asked a question on SF - https://serverfault.com/questions/408805/configuring-httpd-conf-to-handle-wildcard-domains-with-multiple-scripts - from where I used adaptr's technique to pass the domain parameter to the PHP scripts.
My personal favorite for redirecting whole VirtualHosts in apache is to simply create a VirtualHost for the domain to redirect and use the Redirect directive:
<VirtualHost IPADDRESS:80>
ServerName example.com
Redirect / http://www.example.com/
DocumentRoot /var/www # <-- Just for completeness
</VirtualHost>
... and then another VirtualHost for your actual website
Redirect redirects every request going to host a to b while keeping any postfixes (e.g. http://example.com/foo?bar=bak becomes http://www.example.com/foo?bar=bak).
I use Redirect a lot to rewrite from http:// to https://
Wow. 3 hours later... Lots of changes, lots of learnings.
1) Changed this:
NameVirtualHost IPADDRESS:80
To:
NameVirtualHost *:80
2) Marked all:
<VirtualHost IPADDRESS:80>
As:
<VirtualHost *:80>
3) Rearranged ServerName and placed it first within the VirtualHost (not sure if this made any difference)
<VirtualHost *:80>
ServerName test4.example.com
ServerAlias test4.example.com
DocumentRoot /home/test4/public_html
UseCanonicalName On
</VirtualHost>
3) Rearranged all VirtualHosts. Placed the 'static' / fixed subdomains earlier and the catch-all / www one as the last one. The last one looks like:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /var/www/html/abc
UseCanonicalName On
...