htaccess for url redirection with virtualhosts - apache

I have an Apache server running 3 virtual hosts:
<VirtualHost *:80>
DocumentRoot "/var/www/sub1"
ServerName sub1.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/sub2"
ServerName sub2.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/sub3"
ServerName sub3.domain.com
# Other directives here
</VirtualHost>
I would like to set up URL redirection on sub3 so that when sub3.domain.com/go/7dj29 is requested, it redirects to sub3.domain.com/go/redirect.php?id=7dj29.
I've created an .htaccess in /var/www/sub3:
RewriteEngine On
RewriteRule ^\/go\/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1 [QSA,L]
However, I receive a 404 instead of a redirect; what am I doing wrong?
Not Found
The requested URL /go/7uja8 was not found on this server.

The following rule works.
RewriteRule ^go/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1
For apache to load rules from .htaccess you need at least AllowOverride FileInfo, but you can set it directly in VirtualHost context without it.

Related

apache redirect HTTPS to canonical HTTPS

I want all access to my website to be forced to HTTPS (https://support.google.com/webmasters/answer/6073543?hl=en).
I also want to force canonical www URL access (https://www.yes-www.org/why-use-www/)
I am attempting to do so according to Apache recommendations using the Redirect directive https://wiki.apache.org/httpd/RedirectSSL and https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost
I have a valid lets-encrypt certificate which has both www and the naked domain.
I have configured *:80 and *:443 VirtualHost redirects. /etc/httpd/conf.d/www.example.com.conf:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://www.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
Redirect permanent / https://www.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
DocumentRoot "/var/www/html/www.example.com"
</VirtualHost>
<Directory "/var/www/html/www.example.com">
Order allow,deny
Allow from all
</Directory>
All works well if I specify base URL (example.com, www.example.com, https://example.com, etc). However, if I specify a page on the naked HTTPS request the redirect eats the root slash (https://example.com/index.html becomes https://www.example.comindex.html).
I do it with the following for all non-ssl to ssl -
<VirtualHost *:80>
ServerName example.org
ServerAlias www.example.org
RewriteEngine on
RewriteRule ^/(.*)$ https://www.example.org/$1 [R,L]
</VirtualHost>
Slighly different should do the same for https://example.org only redirecting to www.example.org
<VirtualHost your.ip.add.ress:443>
ServerName example.org
RewriteEngine on
RewriteRule ^/(.*)$ https://www.example.org/$1 [R,L]
*snip*
Normal SSL certificate/key stuff goes here
*snip*
</VirtualHost>
RedirectMatch appears to solve the problem similar to the Rewrite suggested by ivanivan. Changing Redirect line in *:443 VHost section to the following seems to fix the issue:
RedirectMatch permanent ^/?(.*) https://www.example.com/$1
I still don't understand why simple Redirect doesn't work with HTTPS.
As an aside, https://salferrarello.com/chrome-clear-redirect-cache/ was useful disabling Redirect caching in Chrome during testing.

Virtual host not working AWS apache

I have following virtual host ( in apache2.conf file ) to load all subdomain from a single directory on AWS
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName mydevsite.com
ServerAlias mydevsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/apps
ServerName mydevsite.com
ServerAlias *.mydevsite.com
</VirtualHost>
It not working and subdomains are still pointing to html directory
I tried following in htaccess as well but then it gives me 500 error
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
My simple requirement is to load main domain from root directory and all subdomains from apps directory
Check the Doc: Using Name-based Virtual Hosts, ServerName Directive, ServerAlias Directive
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.mydevsite.com
ServerAlias mydevsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/apps
ServerName apps.mydevsite.com
ServerAlias *.mydevsite.com
</VirtualHost>

SSL Wildcard Apache

I have a SAAS in which I need to create multiple subdomains dynamically (Note: There are a lot of subdomains, I don't know which ones will be auto generated ). So I canĀ“t repeat this for each one:
<VirtualHost *:80>
ServerName a1.app.example.com
Redirect / https://a1.app.example.com
</VirtualHost>
Is there a way to redirect each one of the subdomains in http to https?. Something like " Redirect / https://*.app.example.com "
<VirtualHost *:80>
ServerName app.example.com
Redirect / https://*.app.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName app.example.com
ServerAlias *.app.example.com
DirectoryIndex index.php
DocumentRoot /var/www/xxxxxxx
SSLEngine on
SSLCertificateFile /etc/ssl/xxxxx.crt
SSLCertificateKeyFile /etc/ssl/xxxx.key
SSLCertificateChainFile /etc/ssl/xxxx.crt
</VirtualHost>
Add that to your main vhost:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,QSA,R=301]
And make sure mod_rewrite is active. You can activate it using the a2enmod rewrite command.

apache2 VirtualHost subdomain configuration not working

All i want is, that www.example.com redirects to example.com, that works, but i can't add another VirtualHost which would allow admin.example.com for admins.
Here are my .conf file:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias old.website.com
DocumentRoot /var/www/redirect
<VirtualHost *:80>
ServerName admin.example.com
DocumentRoot /var/www/admin
and
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
However, if i enter admin.example.com i get redirected to example.com and if i use anything else as subdomain, for example test.example.com or anything.example.com the normal example.com site is shown BUT in the URL field you can still see the original URL (anything.example.com, ...) which isn't the case when using admin.example.com. My .htaccess file in the /var/www/redirect directory looks like that:
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
ErrorDocument 404 /index.html
Ok it seemed to be a google-chrome only error that was caused by my browser data, if anybody else is having problems with it, try using Icognito Mode in Chrome, it should work there.
Thanks to Chand Prakash for helping me

Apache: virtualhost on each sub-domain to corresponding directory

I would like to do something like this:
<VirtualHost *:80>
ServerName $variable.example.com
DocumentRoot /var/www/$variable
</VirtualHost>
for example if I go to foo.example.com it must show me /var/www/foo directory content
What is the good apache syntax for that?
You can't have $variable like that in servername and map it to the document root, but you can use mod_rewrite and a wildcard.
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAlias *.example.com
DocumentRoot /var/www/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*::\1
RewriteRule ^(.*)$ /%1/$1 [L]
</VirtualHost>
The subdomain.example.com can just be any subdomain that isn't www.example.com. For any request that doesn't start with "www", the subdomain is captured as a group (2nd condition), then the next line makes sure the request isn't already being routed into the subdomain's name, and the rule routes it.
So for the request:
http://foo.example.com/bar/x.html
it gets routed to:
/foo/bar/x.html
This is what I use with my freelancer work:
Under httpd-vhosts.conf:
UseCanonicalName Off
<VirtualHost *:${AP_PORT}>
DocumentRoot ${US_ROOTF_WWW}/_client
ServerName client
ServerAlias client
<Directory "${HOME}\www\_client">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:${AP_PORT}>
VirtualDocumentRoot "${US_ROOTF_WWW}/_client/%1"
ServerName subdomains.client
ServerAlias *.client
<Directory "${HOME}\www\_client\*">
Require all granted
</Directory>
</VirtualHost>
Then under the windows hosts file place the domain and subdomains that are needed.
127.0.0.1 client
127.0.0.1 someone.client
127.0.0.1 someone2.client