Change port and host using .htaccess

21,283

Solution 1

Try using this in your .htaccess.

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^sub.website.com:2000 [NC]
RewriteRule (.*) https://123.45.67.891:3000/$1 [R=301,L]

You can also look the accepted answer here (http://www.webmasterworld.com/apache/3264071.htm). R=301 is used for permanent redirection. If you wish to use temporary redirection, use R=302 instead.

If it doesn't help, you can try enabling proxy in your apache: (edit your /etc/apache2/httpd.conf)

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

ProxyRequests Off

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>

<VirtualHost sub.website.com:2000>
    ServerName  redirecting
    ServerAdmin [email protected]

    ProxyRequests off
    ProxyPass / http://123.45.67.891:3000
</VirtualHost>

(look the solution posted here Apache port forwarding).

Solution 2

It's hard to do this with RewriteRules in .htaccess files. You problem is that .htaccess files are only read after the server has gone through the URL-to-filename translation phase. Only after a server has come to the (preliminary) conclusion that the resource it needs to serve exists on it's filesystem will it start reading the relevant .htaccess files. So it might be that your .htaccess file isn't ever read for these requests. Enable rewritelog to find this out.

You should do this in the main server config.

Share:
21,283

Related videos on Youtube

Joshua
Author by

Joshua

Updated on September 18, 2022

Comments

  • Joshua
    Joshua over 1 year

    I am trying to use mod_rewrite to basically port forward a port on a subdomain to another port on another IP.

    Like this:

    sub.website.com:2000 --> 123.45.67.891:3000 
    

    How could this be accomplished using a .htaccess file with mod_rewrite?

    I have tried the following but to no avail:

    RewriteCond %{HTTP_HOST} ^sub.website.com$ [NC]
    RewriteCond %{SERVER_PORT} ^2000$
    RewriteRule ^(.*)$ https://123.45.67.891:3000/$1 [L,R=302]
    
  • Joshua
    Joshua about 11 years
    Thanks but I've had no luck with that, i'm trying it here with port 2000 but Safari just tells me that the server where the page is located is not responding.
  • Meriadoc Brandybuck
    Meriadoc Brandybuck about 11 years
    Did you try to restart apache?
  • Joshua
    Joshua about 11 years
    Still no joy unfortunately
  • Joshua
    Joshua about 11 years
    My .htaacess file: pastebin.com/RB8K1Dhi
  • Krist van Besien
    Krist van Besien about 11 years
    Is your server actually listening to port 2000? If safari times out this usually means that your server isn't responding at all.