Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting rules in Apache for a different port

My Apache(Tomcat)-Spring server runs on port 8080. I want to make a call to localhost default port(80) and want a redirection to take place to port 8080.

I enabled mod-rewrite and the following rule from DigitalOcean works fine.

RewriteRule ^orange.html$ apple.html

I read the rewrite rules of apache from Apache URL Rewriting Doc

However the following rules does not work:

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://localhost:8080/$1 [L,R]

My intention is to allow cross domain support without enabling CORS in spring controller.(This is a hard rule)

The .htaccess is located in /var/www/html. Also I do not want other requests to be redirected on the port 8080 except which are on localhost/(my_specified_string)

like image 976
Nikhil Sahu Avatar asked Jul 13 '15 10:07

Nikhil Sahu


2 Answers

ProxyPass is what was needed here and not rewrite engine.

Following config lines in the 000-default.conf present in /etc/apache2/sites-available did the trick.

ProxyPass /servlet-name http://localhost:8080/servlet-name
ProxyPassReverse /servlet-name http://localhost:8080/servlet-name

This redicrected any request on /servlet-name to http://localhost:8080/servlet-name.

One can replace "servlet-name" above with their respective servlet name.

More info here: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

like image 137
Nikhil Sahu Avatar answered Sep 26 '22 23:09

Nikhil Sahu


You can use this rule in root .htaccess:

RewriteEngine On

RewriteCond %{SERVER_PORT} =80
RewriteRule ^my_specified_string http://localhost:8080%{REQUEST_URI} [NC,L,R]
like image 33
anubhava Avatar answered Sep 26 '22 23:09

anubhava