Match URL to a specific file in Apache - 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]

Related

Apache permanent redirect goes to www automatically

My domain name is example.com without www. So if I put www.example.com then it does not work but example.com works. So I configured apache like this
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAdmin webmaster#example.com
DocumentRoot path/to/project/public
SSLEngine on
SSLCertificateFile /path/to/keys/xxx.crt
SSLCertificateKeyFile /path/to/keys/xxx.key
ErrorLog /var/log/apache2/error_log
CustomLog /var/log/apache2/access_log combined
<Directory "path/to/project/public">
Options FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
Now, as you can see, I do a permanent redirection to https like
Redirect permanent / https://example.com/
But this redirection add www with the domain name by default. So the redirected url becomes https://www.example.com/. Obviously my website can not be accessed from with www since it is registered without www. So please tell me how can make the redirect to work and go to https://example.com/ without the https.
Add an Alias
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
</VirtualHost>
This requires that the www.yourdomain.com points to the same place as yourdomain.com. However the www might not work with your SSL certificate, it depends on the certificates specificity.
I generally allow both on my sites as some people insist on including the www whenever they enter an address.
Apache's documentation can help out with more specifics https://httpd.apache.org/docs/2.2/vhosts/name-based.html
As far as the redirect issue you're having:
Make sure you don't have some RewriteEngine rules that are rewriting your non www requests to www. You might have an .htaccess file in your site directory that is doing the rewrite/redirect.
It might look something like:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R,L]
Which would mean you should remove the www in the last Rewrite Rule

Apache2 redirect with wildcard subdomain

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]

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

How to do an Apache HTTP server virtual host blanket redirect with exceptions

I am running an Apache HTTP server that accepts requests for my domain. Lets call it www.mydomain.com. I have several sites that run under this domain and I have virtual hosts set up for each one that forwards the user depending upon with URL they use. Examples would be a.mydomain.com, b.mydomain.com, etc. I am aware that this may not have been the best way to accomplish this goal considering there are over 100 virtual hosts, but it is something I have inhertited and.. to put it short.. it works. Now on to my problem.
I have recently been tasked with shutting down most of the sites running under the domain. The sites that are shut down have been redirected to one page on my new domain (www.mynewdomain.com). What I have done is changed each of the virtual hosts from something like this:
<VirtualHost *:80>
ServerName a.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteA [PT]
</VirtualHost>
to this:
<VirtualHost *:80>
ServerName a.mydomain.com
Redirect permanent / http://www.mynewdomain.com/replacement
</VirtualHost>
So now I have a list of 100 hosts that all redirect to the same place. What I need to know is if there exists a way to tell the system to redirect all sites at *.mydomain.com to my new page except certain ones. So some entry like this:
<VirtualHost *:80>
ServerName *.mydomain.com
Redirect permanent / http://www.mynewdomain.com/replacement
</VirtualHost>
<VirtualHost *:80>
ServerName h.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteH [PT]
</VirtualHost>
<VirtualHost *:80>
ServerName v.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteV [PT]
</VirtualHost>
Which means that every URL you see will go to "http://www.mynewdomain.com/replacement" except "h.mydomain.com" and "v.mydomain.com". The virtual hosts for "h" and "v" must still be intact because I have RewriteRules for each that must continue to work (additional rewriterules not seen in my examples).
Thanks in advance for the assistance!

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
...