I have some subdomains I want to redirect to specific ports on the same server. Say I have


I want dev.mydomain.com to transparently redirect to mydomain.com:8080 and I want to preserve the original sub-domain name the url of the browser.

How do I do this with Apache 2.2? I have Apache 2.2 running on default port 80. I can't figure out the write configuration to get this to happen.

I have already set up dev.mydomain.com to resolve in DNS to mydomain.com.

This is for an intranet development server that has a non-routable ip address so I am not so concerned about exploits and security that would compromise a publicly facing server.


5 Answers 5



Here is what I finally came up with after being set in the right direction by Miles Erickson. I wanted the address bar to reflect the original subdomain/domain of the request and not the redirected server and port, but he put me on the right path to Google up a solution using VirtualHost and I finally found a solution that included the use of mod_proxy.

First, make sure mod_proxy is enabled:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo systemctl restart apache2

Next, add the following to your site config (e.g., /etc/apache2/sites-available/000-default.conf):

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName dev.mydomain.com
    ProxyPreserveHost On

    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    ProxyPass / http://localhost:8888/
    ProxyPassReverse / http://localhost:8888/
  • 9
    For everyone who this answer didn't work for, keep in mind that you have to enable mods mod_proxy as well as proxy_http.
    – matewka
    Commented Nov 20, 2015 at 19:47
  • What about in nginx ?
    – rosefun
    Commented Dec 1, 2020 at 14:41
  • For macOSX, don't waste hours like me trying to edit /etc/apache2/extra/httpd-vhosts.conf. My httpd.confwasn't looking at this vhosts file by default. I had to add my new conf to /private/etc/apache2/other (ref). Everything else above worked as is.
    – NP01
    Commented Jul 5, 2021 at 0:07

Run the following line on terminal (specify your domain and sub domain name correctly)

sudo nano /etc/apache2/sites-available/subdomain.example.com.conf 

Paste the following code and change as your requirement

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName subdomain.example.com
        ServerAlias subdomain.example.com
        ProxyRequests Off

        #ProxyPass / http://localhost:8080/
        <Location />
                ProxyPreserveHost On
                ProxyPass http://example.com:8080/
                ProxyPassReverse http://example.com:8080/
     # Uncomment the line below if your site uses SSL.
     #SSLProxyEngine On

Run the following lines on terminal (specify your domain and sub domain name correctly)

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2ensite subdomain.example.com.conf
sudo service apache2 restart
  • 5
    Is there any essential difference between this and the accepted answer?
    – kasperd
    Commented Jan 18, 2016 at 10:13
  • 1
    Most of the people forget to add sudo a2enmod proxy_http so they face difficulties on running properly. Also I try to give full process from start to end in a sequence to help more acculturate. Thanks for your question. Commented Jan 18, 2016 at 10:34
  • Thanks kasperd for asking it's just a little bit tweaking for people like me who forget some little things which are essential like sudo a2enmod proxy and sudo a2enmod proxy_http command after creating the configuration file. Commented Feb 7, 2017 at 9:07
  • 1
    This just does not work for me. 'sudo a2enmod subdomain.domain.com.conf' says "ERROR: Module x.y.z does not exist!" Does it have to be mentioned in the 'default' configuration? Commented Apr 17, 2017 at 9:17
  • 2
    Correction in registering subdomain sudo a2ensite sub.yourdomain.com
    – Ali Azhar
    Commented Aug 1, 2017 at 19:53

Assuming that dev.mydomain.com can be resolved to mydomain.com's IP, you could add the following to your httpd.conf:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName dev.mydomain.com
    redirect / http://mydomain.com:8080/

Relevant Apache documentation:

  1. Guide to creating name-based virtual hosts
  2. Core, including VirtualHost and NameVirtualHost
  3. Redirect

Related question: Apache redirect based on hostname

(Note: the original version of this answer incorrectly suggested the use of RedirectMatch, which, as @ChrisS helpfully pointed out, cannot parse the domain portion of the URL.)

  • 1
    see my self provided answer for what I actually used, which is this idea of VirtualHost with mod_proxy to preserve the original host instead of doing a redirect. This is an internal server so I am not concerned with security or exploits of mod_proxy.
    – user35861
    Commented Oct 28, 2010 at 15:38
  • 1
    Found numerous other suggestions but this ultimately worked for me on Windows Server 2008 R2/Apache 2.2.25.
    – HPWD
    Commented Jul 12, 2015 at 20:07
  • 1
    The problem with this solution is that in your brother, you don't stay on the first domain, you get a 302 to the second.
    – Pleymor
    Commented Feb 9, 2017 at 12:20

Add in your main vhost configuration the following lines:

ProxyPreserveHost On
ProxyPass / http://example.com:8080/
ProxyPassReverse / http://example:8080/

Note that this requires mod_proxy on Apache.

  • This is not a redirect; it proxies the connection. Enabling mod_proxy can be extremely easy to exploit if you aren't very careful configuring it. I would highly recommend against this.
    – Chris S
    Commented Oct 28, 2010 at 12:38
  • 4
    He doesn't want a redirect, as per his OP. He wants the original URL to remain in the address bar; this means using either mod_proxy or mod_rewrite, and y'all better avoid mod_rewrite.
    – adaptr
    Commented Mar 1, 2012 at 14:40

You're looking for mod_rewrite. Here's the link to Apache's documentation which includes many examples for basic and advanced configurations..

And if you're unable to interpret the documentation yourself, try adding this to httpd.conf:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^dev\.example\.com$ [NC]        
RewriteRule ^(.*)$ http://example.com:8080$1 [R=301]    

And if that's not a clear example, here's a link to a mod_rewrite beginners guide too.

  • 1
    I'm on shared hosting. Rewrite is the only option. I can create a .htaccess file, but not a proxy or virtual host.
    – abalter
    Commented Feb 15, 2018 at 17:18

You must log in to answer this question.