Apache Virtual Hosts and mod_rewrite conflicts - mod-rewrite

I've been scouring the net and SO and I can't get around or through this problem.
We have a bunch of subdomains and a few dedicated servers. One server does double-triple duty as issue tracking and landing page. Problem is the wildcard landing page doesn't take you to the correct virtual host page sometimes. I've been looking at wildcards but they seem particularly broad.
Our scenario is the following:
-www.askia.com is the main landing site. A non-existing (wildcard) subdomain should always land here.
-dev.askia.com is the technical support and issues site. It has some mod_rewrites for https. It took me a while, but I got it to work and I'd rather not break it.
-www.askia.fr is our french site. Instead of taking you to www.askia.com it takes you to the dev.askia.com.
-www.askia.co.uk should take you to www.askia.com but it goes to dev.askia.com
I'm not entirely sure where I should be trying to fix the solution. Should I do something in the CNAME. In the virtualhosts config file or in the mod_rewrite file.

Try these rules:
RewriteCond %{HTTP_HOST} ^dev\.
RewriteCond %{HTTP_HOST} !^dev\.askia\.com$
RewriteRule ^ http://dev.askia.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.askia\.com$
RewriteCond %{HTTP_HOST} !^dev\.askia\.com$
RewriteRule ^ http://www.askia.com%{REQUEST_URI} [L,R=301]
The first rule redirects every request to a host starting with dev. but not dev.askia.com to www.askia.com. And the second rule redirect requests to a host other than www.askia.com and dev.askia.com to www.askia.com. So every request should either go to dev.askia.com or www.askia.com.

When using Virtual Hosts in Apache the first hosted listed will always be the default for non-matches.
#default vhost
# any non-matches will land here
<VirtualHost _default_:80>
ServerName www.askia.com:80
DocumentRoot /path/to/site
ErrorLog /path/ti/sites/logs/error_log
</VirtualHost>
# vhost #2
<VirtualHost _dev_Site_:443>
ServerName dev.askia.com:443
DocumentRoot /path/to/dev/site
ErrorLog /path/to/dev/sites/logs/error_log
#ssl details
SSLEngine on
SSLCipherSuite HIGH:MEDIUM
SSLCertificateFile /location/securti.crt
SSLCertificateKeyFile /location/securti.key
#any rewrite rules to apply only to this (default) domain
# force SSL for instance..
RewriteRule .* - [F]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://dev.askia.com/
</VirtualHost>
#etc, etc

Related

How can I block apache2 root path to a proxy? is it even possible?

So I am hosting an icecast server and I want to proxy ssl termination to icecast using apache2.
I was using an nginx proxy to icecast which was perfect but there were some complications with nginx and php, so i have to use apache2.
so I am trying to achieve the same proxy as what I achieved in nginx but I simply cannot block the root path without blocking everything or it simply not working.
here is my virtualhost below, I have added some comments for some attempts (2 of about 100 attempts lol)
<VirtualHost *:8050>
ServerAlias *.mydomain.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/mydomain.com.pem
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/stats.xsl [NC]
RewriteCond %{REQUEST_URI} ^/index.html [NC]
# this doesnt work. I expect because of the Rewrite Rule below?
#RewriteCond %{REQUEST_URI} ^/ [NC]
RewriteRule .* - [F,L]
# this doesnt work it blocks everything
#<Location />
# Order Allow,Deny
# Deny from all
#</Location>
ErrorLog ${APACHE_LOG_DIR}/mediaserverproxy-error.log
CustomLog ${APACHE_LOG_DIR}/mediaserverproxy-access.log combined
ProxyPass / http://localhost:80/
ProxyPassReverse / http://localhost:80/
</VirtualHost>
Just to be clear I know you can do ssl in icecast but it reduces stability when sending icecast HUP signals for reloads.
Any help is massively apreciated
What is the expected outcome?
That a request for / will return a 404 and any other request will serve that path with a 200?

How can I use mod_rewrite to redirect multiple hosts between domains?

I'm trying to redirect requests for mis-spelt domain names to the same server on the official domain.
My apache configuration looks like:
<VirtualHost *:80>
RewriteEngine on
# Fix domain spellings in host.<backupdomain>
RewriteCond %{HTTP_HOST} !([^.]+).example.com [NC]
RewriteRule ^/(.*) http://%1.example.com/$1 [NC,R,L]
</VirtualHost>
I know I'm close, because the requests to server99.wrongdomain get re-written to .example.com - and I'm expecting it to go to server99.example.com.
Why isn't the regex capture/expansion working correctly here?
P.S. Incredibly annoying that SO is blocking my original examples because they look like links (!)
If you want to match something not followed by something else then you can use Negative lookahead.
RewriteCond %{HTTP_HOST} ^([^.]+)\.(?!example\.com) [NC]
RewriteRule ^/(.*)$ http://%1.example.com/$1 [NC,R,L]
This way, each wrong domain (with server99 for example)
server99.example.co.uk
server99.exampel.com
etc
will redirect to server99.example.com.
That is virtually everything you need in case you only want to redirect misspelled subdomains.
<VirtualHost *:80>
ServerName *.example.com
RedirectMatch 301 /(.*) http://www.example.com/$1
</VirtualHost>
If you want to redirect every request for which there is no uniqe VHost configuration, use the following and make sure it is the very first VHOST configuration loaded by apache
<VirtualHost _default_:80>
RedirectMatch 301 /(.*) http://www.example.com/$1
</VirtualHost>
Of course that only works if the DNS record of the FQDN points to the apache in question.

Subdomain Mod-Rewrite issue Internal redirection doesn't work

I am trying to get Subdomains work for my Site using htaccess, For example:
shopping.site.local should lead the user to site.local/shopping-and-fashion keeping the initial subdomain format, means in an internal redirection that the user can't notice.
I have added the following script:
RewriteCond %{HTTP_HOST} ^shopping.site.local$ [OR]
RewriteCond %{HTTP_HOST} ^www.shopping.site.local$
RewriteRule ^(.*)$ http://www.site.local/shopping-and-fashion/$1 [L]
The redirection happens successfully but in an external way, how to hide the redirection to have always the url:
http://shopping.site.local
and the content of:
http://www.site.local/shopping-and-fashion
Did you tried to use [P] instead of [L] ?
The following uses reverse proxying and should give you what you want
<VirtualHost *:80>
ServerName shopping.site.local
ProxyPass / http://www.site.local/shopping-and-fashion/ nocanon
ProxyPassReverse / http://www.site.local/shopping-and-fashion/
</VirtualHost>
You might have to hack some JS and CSS to accommodate for the path changes if the original files are not relative links friendly.

mod_rewrite getting rid of port number in favor of directory

How do I mod_rewrite ip:4040 to ip/streamer ?
I have application at ip:4040 but it is hard for users to remember port number.
So I created directory /var/www/streamer
And now I'm creating apache virtualhost:
<VirtualHost *:80>
DocumentRoot /var/www/streamer
RewriteEngine On
RewriteRule / :4040
</VirtualHost>
Obviously rewriteRule is incorrect, but having no idea of regex, any thoughts on how to do this?
Assuming your web server is also listening on port 80, as well as 4040:
RewriteEngine On
RewriteCond %{HTTP_HOST} 4040
RewriteRule .* http://xxxxxxx/ [L,R=301]
Where xxxxxxx is the server host name or IP, without a port number.

mod_rewrite don't match on RewriteCond target - URL not found

I am trying to rewrite all URIs inside directory /docs to /docs/index.php, using apaches mod_rewrite module.
The URI doesn't exist as a directory structure, which I don't think is necessary when you are rewriting the URI to something that exit.
I am getting the error:
"The requested URL /docs/view/my_doc was not found on this server."
These are my rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
I am doing the configuration in httpd.conf. Also, I am using a multi v-host setup.
What's my problem and how do I fix it?
I'm using Apache/httpd on CentOS 6.2
UPDATE
Including my VirtualHost as well:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.smicloud.org
ServerAlias *.smicloud.org
VirtualDocumentRoot /var/www/html/www.smicloud.org
ExpiresActive On
ExpiresDefault "access plus 14 days"
# insert logging config, anything else you need..
#Define FileEtag so it doesnt use node
FileETag MTime Size
<Directory /var/www/html/>
Order Allow,Deny
Allow from all
# Get rid of this if you need to allow htaccess files:
AllowOverride None
</Directory>
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
</VirtualHost>
This should work
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
Ok, there were a couple of things that I had to do in order to fix it, using #donalds suggestion (which I actually tried before):
1 - I had to have a '?' at the end of the regex of RewriteRule:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
Then I can chose whether to have a '/' or not at the end of the address.
2 - I had to add a '/' to the beginning of the regex of my RewriteRule. I.e.:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
3 - I also had to add the 'http://%{HTTP_HOST}/' part to the new URI:
http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
This is what I ended up with:
RewriteRule ^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
Perhaps why I needed to add the %{HTTP:HOST} was because it's a multi virtual host setup.

Resources