I am using apache in front of tomcat so I can redirect requests from the domain to the tomcat url using mod_proxy. I've run into an issue on the server where when using a subdomain it wants to go back to the base domain. Is there any way to redirect all requests from example.com/foo to foo.example.com?
<VirtualHost *:80>
ServerName example.com/app
ProxyPreserveHost Off
ProxyPass / http://app.example.com:8080/AppName/
ProxyPassReverse / http://app.example.com:8080/AppName/
</VirtualHost>
The example above is not working. I presume that is because I cannot include a path in the servername.
Here is my working version of the tag based on the accepted answer.
<VirtualHost *:80>
ServerName example.com/app
ProxyPreserveHost Off
ProxyPass / http://app.example.com:8080/AppName/
ProxyPassReverse / http://app.example.com:8080/AppName/
# Rewrite all request from example.com/foo to foo.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/foo/?(.*) http://foo.example.com/$1 [R,L]
</VirtualHost>
Speaking strictly to the last statement in your original post, "Is there any way to redirect all requests from example.com/foo to foo.example.com".
You can use a rewrite to accomplish this. For example:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/foo/?(.*) http://foo.example.com/$1 [R,L]
This assumes that your rewrite module is loaded and enabled:
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
You could use mod_rewrite within apache. Ensure it is enabled, as root:
a2enmod rewrite
That will confirm it is has been enabled or already is. If it has been enabled you'll need a restart of Apache.
Then in your root of your virtualhost create/edit .htaccess file and add the line:
Redirect /foo/ http://foo.example.com/
Depending on your specific directory permissions you may need to allow overrides for the directory
<Directory /your/path/here>
AllowOverride All
</Directory>
HTH
Related
I need help about a configuration. I have an .htaccess for my frontend webserver which is so configured:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/?(dir_a|dir_b|dir_c)
RewriteCond %{QUERY_STRING} !^/?(dir_a|dir_b|dir_c)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
DirectoryIndex index.php
So, dir_a, dir_b and dir_c, which are on the frontend server, are not redirected to https. Everything on the frontend server is correctly redirected to https if https is omitted or using http when one inputs an URL of my website. This does not happen for a location pointing to an internal webserver, i.e I have in my apache2.conf:
<VirtualHost *:443>
...
ServerName example.com
SSLEngine on
SSLProxyEngine On
...
</VirtualHost>
...
<Location /backsrvdir>
SSLRequireSSL
ProxyPass http://192.168.x.y/backsrvdir
ProxyPassReverse http://192.168.x.y/backsrvdir
</Location>
In backsrvdir I have another .htaccess with its DirectoryIndex bsindex.php.
It works only if the link already contains https:, so if I write or click on https://example.com/backsrvdir it's ok, if omit https: or using http: the frontend server responds with a "403 Forbidden: You don't have permission to access /backsrvdir/ on this server. Apache/2.2.22 (Debian) Server at example.com Port 80".
As I stated above, port 80 is open only for dir_a dir_b and dir_c.
Any idea to solve the problem and have http://example.com/backsrvdir redirected to https://example.com/backsrvdir?
Thanks in advance.
Try something like this:
<VirtualHost *:80>
...
Redirect permanent /backsrvdir https://example.com/backsrvdir
# Remove the other 3 lines:
# SSLRequireSSL
# ProxyPass http://192.168.x.y/backsrvdir
# ProxyPassReverse http://192.168.x.y/backsrvdir
...
</VirtualHost>
Also remove anything related to https redirection from .htaccess
This question already has answers here:
http to https through .htaccess
(17 answers)
Closed 6 years ago.
How to tell if http://ttt.com or http://www.ttt.com is used by the user, redirect it to https://www.ttt.com ?
httpd.conf:
<VirtualHost *:80>
ServerName www.ttt.com
ServerAlias ttt.com
DocumentRoot /home/www/html/ttt/public
<Directory /home/www/html/ttt/public>
#Options ExecCGI
#AddDefaultCharset utf-8
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
.htaccess :
RewriteEngine On
############################################
## always send 404 on missing files in these folders
#RewriteCond %{REQUEST_URI} !^/(files)/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Try the following Code at the main directory .htaccess file :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
You can do it from the httpd.conf with the following:
<VirtualHost *:80>
ServerName www.ttt.com
Redirect "/" "https://www.ttt.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName www.ttt.com
...
...
</VirtualHost>
Or from the .htaccess file:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Here you go: https://wiki.apache.org/httpd/RedirectSSL
Redirect Request to SSL
Let's say you want http://www.example.com/secure/ to always be sent
over SSL (I presume here that both the normal and the SSL vhost have
the same content). You could do this by linking to the correct page
from within your HTML pages... but there will always be some user who
will sneak by it that way.
Using virtual hosts (using redirect)
When using SSL, you will frequently have at least two virtual hosts:
one on port 80 to serve ordinary requests, and one on port 443 to
serve SSL. If you wish to redirect users from the non-secure site to
the SSL site, you can use an ordinary Redirect directive inside the
non-secure VirtualHost:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
Redirect permanent /secure https://mysite.example.com/secure
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
When redirecting everything you don't even need a DocumentRoot:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://secure.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName secure.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Note: redirect can also be used inside .htaccess files or to address
particular URLs, as in:
Example:
Redirect permanent /login https://mysite.example.com/login
Using mod_rewrite
While the solution is recommended because it is simpler
and safer, you can also use mod_rewrite to get the same effect as
described here: RewriteHTTPToHTTPS
From https://httpd.apache.org/docs/trunk/mod/mod_alias.html#redirect:
# Redirect to a URL on a different host Redirect "/service" "http://foo2.example.com/service"
# Redirect to a URL on the same host Redirect "/one" "/two"
If the client requests http://example.com/service/foo.txt, it will be
told to access http://foo2.example.com/service/foo.txt instead. This
includes requests with GET parameters, such as
http://example.com/service/foo.pl?q=23&a=42, it will be redirected to
http://foo2.example.com/service/foo.pl?q=23&a=42. Note that POSTs will
be discarded. Only complete path segments are matched, so the above
example would not match a request for
http://example.com/servicefoo.txt. For more complex matching using the
expression syntax, omit the URL-path argument as described below.
Alternatively, for matching using regular expressions, see the
RedirectMatch directive.
If you want both the www.example.com/* and the example.com/* to be redirected you could make two VirtualHost with different ServerName or you can use the Rewrite plugin.
Currently, I have the following rule in my httpd.conf file to forward all requests from port 80 to port 8080 to be served by GlassFish app server:
<VirtualHost *:80>
ServerAdmin admin#myserver.com
ServerName myserver.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
Now, I need to add a rule such that all requests to http://myserver.com/ will be forwarded to http://myserver.com/page/index.html and the URL should still appear to be http://myserver.com/ on the client's browser. I tried to add the following rules inside the above VirtualHost:
RewriteEngine On
RewriteRule http://myserver.com/ http://myserver.com/page/index.html
or
RewriteEngine On
RewriteRule ^/ http://myserver.com/page/index.html
or
RewriteEngine On
RewriteRule ^/index.html http://myserver.com/page/index.html
However, when I go to http://myserver.com/, the browser have this error: This webpage has a redirect loop. The 3rd rule can only work if I go to http://myserver.com/index.html.
I am a total noob at writing rules for Apache. Hence, I'd be very grateful if you could show me what I've done wrong here :).
UPDATE:
The following rule works perfectly:
RewriteEngine On
RewriteRule ^/$ /page/index.html [R]
You need to add a $ indicating the end of the URI:
RewriteEngine On
RewriteRule ^/$ http://myserver.com/page/index.html
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
Without the $, the regex ^/ matches /page/index.html which will cause it to redirect again, and it'll match again, and redirect again, etc.
I want to make sure all my traffic is on ssl even if they type http. But I also want it to pass the folders so mod_rewrite will still work. I tried this poor example but it does not work. Basicly I if they type http://mydomain.com/apage it will redirect to https://mydomain.com/apage
Server: Apache2, LAMP stack.
.htaccess
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(/) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
I am open to tweaking a virtual host files for Apache but I have not seen it done like that before. This is my first adventure into ssl hosting.
Just replacing http with https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I suggest not using mod_rewrite or htaccess if you have access to httpd.conf.
If you want to force all users to use https (a good idea) you can add something like this to httpd.conf:
<VirtualHost 1.2.3.4:80>
ServerName SSL.EXAMPLE.COM
CustomLog /var/log/httpd/EXAMPLE.access_log combined
ErrorLog /var/log/httpd/EXAMPLE.error_log
Redirect / https://ssl.example.com/
</VirtualHost>
<VirtualHost 1.2.3.4:443>
ServerName ssl.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM
.
.
.
</VirtualHost>
<Directory /var/www/html>
#If all else fails, this will ensure nothing can get in without being encrypted.
SSLRequireSSL
</Directory>
I have a problem trying to rewrite the root of my web from mywebsite.com/mywebsite to www.mywebsite.com/mywebsite.
The problem is related to the use of mod_proxy to invoke Tomcat
File proxy.conf
<ifmodule mod_proxy.c>
ProxyRequests Off
ProxyPreserveHost On
<proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</proxy>
ProxyVia On
ProxyPass /mywebsite ajp://91.222.222.222:8009/mywebsite
ProxyPassReverse /mywebsite ajp://91.222.222.222:8009/mywebsite
<location miaplicacion>
Order allow,deny
Allow from all
</location>
</ifmodule>
File .htaccess
RewriteCond %{HTTP_HOST} !^www.mywebsite.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [NC,L,R=301]
The point is that the rule works OK if I write mywebsite.com/something. In this case it's correctly redirected to www.mywebsite.com/something
However It seems as if the proxy has preference over what is written in the rewrite rules. That is to say, when it finds mywebsite.com/mywebsite, instead of rewriting it to www.mywebsite.com/mywebsite and then invoke Tomcat, it calls it inmediately without touching the URL.
Do you know any way to force the rewriting of the URL before proxying to Tomcat?
Try this as your first line:
RewriteCond %{HTTP_HOST} ^mywebsite.com$ [NC]