I'm trying to get Apache's RewriteEngine to rewrite some URL's to Tomcat Webapp Directories.
It already works partially, but there is one thing that I cannot achieve.
This is the scenario:
I have apache and a tomcat running on a vps.
On the tomcat I have a webapp called XYZ.
On Apache, I use the jk_mod module with workers.properties to "connect" to the tomcat.
In my httpd.conf I have a VitualHost set up as follows:
<VirtualHost *:80>
ServerName abc.domain.com
RewriteEngine on
RewriteRule ^/(.+)$ /XYZ/$1 [L,PT]
JkMount /* XYZ
</VirtualHost>
So far, so good. Without the RewriteRule I'd have to open the URL like this:
abc.domain.com/XYZ/home (where "home" is the jsp)
abc.domain.com/XYZ/customers
etc...
And the WITH the RewriteRule I'm down to this:
abc.domain.com/home
abc.domain.com/customers
etc...
Which is already pretty good for my goal, but there is just one more thing that needs to be done: I want to be able to open abc.domain.com and to be redirected to the last example above (home jsp).
I've tried adding some other RewriteRules that would rewrite the root to /XYZ/home and also tried a Redirect, but none of those worked as I expected them to (nothing happened)...
Can somebody please explain, how I can achieve this?
Thank you very much in advance!
I got it to work in the end. Using a second RewriteRule. I guess that I had some mistakes in my wildcard / regex in my previous tries.
This is what works:
<VirtualHost *:80>
ServerName abc.domain.com
RewriteEngine on
RewriteRule ^/(.+)$ /XYZ/$1 [L,PT]
RewriteRule ^/$ /XYZ/home [L,PT] # <---- This
JkMount /* XYZ
</VirtualHost>
During my research I also learnt that a Redirect 301 / /home will basically redirect everything, NOT only the root, as would have needed it to.
Put the Redirect above the RewriteEngine clause.
Something like:
<VirtualHost *:80>
ServerName abc.domain.com
Redirect 301 ^/$ /home
RewriteEngine on
RewriteRule ^/(.+)$ /XYZ/$1 [L,PT]
JkMount /* XYZ
</VirtualHost>
Related
I need a rule to internally rewrite several domains, with and without www:
www.a.com --> /m/n/o/
b.c.org --> /x/y/z/
The setup is Apache running locally on Windows (XAMPP). I've got the hosts file set up so all the domains point to localhost. I'd like every page to get redirected, i.e. I want to point each domain to it's own different root directory and have it work normally from there. e.g.
/ <-- Top level folder, everything is under here.
/root/of/domain/A/ <-- www.a.com
/root/of/domain/C/ <-- b.c.org
You have two choices.
(1) The one you asked (with mod_rewrite)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?a\.com$ [NC]
RewriteRule ^/(.*)$ /root/of/domain/A/$1 [L]
RewriteCond %{HTTP_HOST} ^b\.c\.org$ [NC]
RewriteRule ^/(.*)$ /root/of/domain/C/$1 [L]
</IfModule>
Note: don't forget to replace example values by real ones. Also, make sure mod_rewrite is enabled.
(2) the cleanest way: configure virtualhosts directly (without mod_rewrite)
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "X:/path/to/root/of/domain/A/"
ServerName a.com
ServerAlias www.a.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "X:/path/to/root/of/domain/C/"
ServerName b.c.org
</VirtualHost>
I have a domain, lets say mydomain.com.
I want every subdomain of this to load the contents of the directory with the same name... For example, if someone writes
http://sub.mydomain.com/index.php
I want to show him the contents of
http://sub.mydomain.com/sub/index.php
Still, I want it to show in the addressbar the http://sub.mydomain.com/index.php
The apache vhost is like
<VirtualHost *:80>
ServerName *.mydomain.com
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
so in the filesystem, the files for the example above would be under the directory
/var/www/vhosts/mydomain.com/subdomains/subs/www/sub
I tried many of the proposed solutions here, but most of them were redirects to some other domain/subdomain or end up in a redirect loop :(
tia
You need to add some rewrite rules that will examine the sub domain and then rewrite to the relevant path:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com
RewriteRule ^/(.*)$ /%1/$1
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
What this will do is capture anything preceding ".mydomain.com" then rewrite it into the URL as %1, $1 will be the requested resource such as index.html
Be aware this might trip you up if www.mydomain.com is a valid domain for your site!
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]
I am trying to always redirect people to
https://www.somedomain.com/URL
when they come in on a non secure port. This is because my SSL is for this url.
When someone goes to http://www.somedomain.com they get sent to
http://www.www.somedomain.com
Here is the htaccess rewrite that i am trying:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}
Edit:
I am using a cPanel server, so making additional hosts is not going to work.
I am trying to always redirect people to https://www.somedomain.com/URL
I think this is all you need:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://www.somedomain.com/$1 [R=301,L,NC]
You'll want to not unconditionally add the www, if it already exists.
See: Prepend 'www' to an HTTPS url using .htaccess & mod_rewrite
Note that mod_rewrite is not the preferred way to do that. Using virtualhosts it's simpler. Just put this configuration (Source: apache wiki):
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.somedomain.com
Redirect permanent /secure https://www.somedomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.somedomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
I want my apache to always force people to use https and to map IP based look ups to be forwarded to the server name. The following settings (in httpd.conf file) take care of the http to https redirect:
<Location />
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://my_server.example.com%{REQUEST_URI}
</Location>
But now I also want that if people type 192.168.1.2 they get redirected to my_server.example.com.
To sum up:
http://192.168.1.2/someurl -> https://my_server.example.com/someurl
I've tried several things but either my settings get ignored or I end up in a redirect loop.
any hints?
If you have access to the main config and not just .htaccess, this is something most easily done with separate virtual hosts rather than resorting to the Swiss Army chainsaw that is mod_rewrite.
<VirtualHost *:80>
Redirect permanent / https://my_server.example.com/
</VirtualHost>
<VirtualHost *:443>
Redirect permanent / https://my_server.example.com/
SSLEngine on
</VirtualHost>
<VirtualHost *:443>
ServerName my_server.example.com
SSLEngine on
...real site config...
</VirtualHost>
It's not just numeric IP address access you generally want to redirect to your canonical hostname, but all addresses other than the known-good domains you control. Putting a default virtual host first in the config, that redirects (or serves up nothing) helps avoid certain DNS-based XSS attacks.
I figured out the following solution:
<Location />
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my_server\.example\.com [NC]
RewriteRule (.*) https://my_server.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://my_server.example.com%{REQUEST_URI}
</Location>
it does exactly what I need.
I'm really new to apache mod_rewrite module. I have a page called http://abc in my company intranet. I want users to be redirected to http://abc.somecompanyname.com whenever they type http://abc to the URL bar. Could someone please provide and example or point me in the right direction.
I figure this should be quite an easy question to answer. Thanks everyone for you inputs.
-Mark
Quote from Apache 2.4 documentation:
The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).
<VirtualHost *:80>
ServerName undesired.example.com
ServerAlias example.com notthis.example.com
Redirect / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
</VirtualHost>
This does require another virtual host, but there's no shortage of those. The solution works very well for me - and I like how redirection of 'unwanted' hosts and configuration of the canonical host are separated.
You could accomplish that with a VirtualHost definition as simple as this, on the server handling requests for abc:
<VirtualHost *:80>
ServerName abc
RewriteEngine on
RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>
I found the advise in the Apache2 URL Rewriting Guide worked better.
I ended up with:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://foo.bar.com/$1 [L,R]
The "RewriteEngine on" line wasn't included in the Apache2 example. Maybe it's usually on by default but in my case I needed to add it.