Forum Moderators: phranque

Message Too Old, No Replies

Redirect incoming url to new url

Redirect url

         

Ralivo

10:17 am on Dec 2, 2010 (gmt 0)

10+ Year Member



Hi,

I am looking for the best way to achieve the following. I have three webservers on one physical host on three separate ports:

[uat1.example.com:11010...]
[uat2.example.com:11020...]
[uat3.example.com:11030...]

I want to deny or redirect access if the wrong url is specified on the wrong port i.e if uat1.example.com:11030 is specified using the port for uat3 then the users can get to port 11030 but get a certificate error. I want to disallow this access or redirect all urls to the correct url. I have tried rewrite rules and redirects but not been able to get this right as yet. The webserver names are all 3DNS names and span two physical servers. Any help would be appreciated.

Thanks !

g1smd

11:19 am on Dec 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



HTTP_HOST contains the requested host name.

SERVER_PORT contains the requested port number.

What code have you tried so far?

Ralivo

1:13 pm on Dec 2, 2010 (gmt 0)

10+ Year Member



Hi,

I have tried some of the following below .... plus a few others but not been able to get it to work

#Options +FollowSymLinks
#RewriteEngine on
#RewriteRule ^/(.+)\.*$ [uat3.example.com:10035...] [R,NC]
#RewriteRule ^https://uat2.example.com$ [uat3.example.com...]
#RewriteCond %{HTTP_HOST} !^uat1\.example\.com [NC]
#RewriteCond %{HTTP_HOST} !^$
#RewriteCond %{SERVER_PORT} !^10035$
#RewriteRule ^/(.*) [uat3.example.com:%{SERVER_PORT}...] [L,R]

jdMorgan

1:38 am on Dec 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# If NOT any valid port/hostname combination, deny access
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^11010>uat1\.example\.com
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^11020>uat2\.example\.com
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^11030>uat3\.example\.com
RewriteRule ^ - [F]

Note that the ">" character is arbitrary; it is used only as a 'soft anchor' to demarcate the two parameters checked by each RewriteCond.

Jim

Ralivo

6:12 pm on Dec 13, 2010 (gmt 0)

10+ Year Member



Thanks for the update !

I tried this but still could not get it to work

RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^10025>uat2\.example\.com
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^10025>uat2\.3dns\.example\.com
RewriteRule ^ - [F]

As I need to make provision for the 3DNS server name that does a webserver healthcheck every 30 seconds. Do i need to implicitly add the other webservers names? (uat1 & uat3) to the check as i can still get through to port 10025 using [uat1.example.com:...] but get the certificate error?

jdMorgan

1:35 am on Dec 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Based on your original post, you need to use the code exactly as I posted it -- including *all* valid hostnames. If you remove any, as you did when you modified my code, then of course it won't work.

Add the "3dns" is trivial. Just modify the existing patterns to make that sub-subdomain optional:

RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^10025>uat2\.(3dns\.)?example\.com

Jim

Ralivo

10:47 am on Dec 14, 2010 (gmt 0)

10+ Year Member



Ok thanks getting there now ... i think. Each of the three webservers is a virtualhost within a httpd.conf file of its own. All are using mod_proxy. I have the following in uat2 virtualhost section
<VirtualHost 0.0.0.0:10025>
Options +FollowSymLinks -MultiViews
AcceptPathInfo off
RewriteEngine On
RewriteLog /tmp/rewrite.log
RewriteLogLevel 3
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^10015>uat1\.(3dns\.)?example\.com [NC]
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^10025>uat2\.(3dns\.)?example\.com [NC]
RewriteCond %{SERVER_PORT}>%{HTTP_HOST} !^10035>uat3\.(3dns\.)?example\.com [NC]
RewriteRule ^ - [F]

I now get this in the rewrite log file

165.?.?.? - - [14/Dec/2010:10:23:32 +0000] [uat1.example.com/sid#af04e68][rid#afdbd58/initial/redir#1] (2) init rewrite engine with requested uri /error/HTTP_FORBIDDEN.html
165.?.?.? - - [14/Dec/2010:10:23:32 +0000] [uat1.example.com/sid#af04e68][rid#afdbd58/initial/redir#1] (3) applying pattern '^' to uri '/error/HTTP_FORBIDDEN.html'
165.?.?.? - - [14/Dec/2010:10:23:32 +0000] [uat1.example.com/sid#af04e68][rid#afdbd58/initial/redir#1] (2) forcing responsecode 403 for /error/HTTP_FORBIDDEN.html

It gets repeated 10 times and the webserver errorlog has the following error

[Tue Dec 14 10:38:43 2010] [error] [client 165.?.?.?] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

I also get a 500 error that is coming from an internal check i assume. I thought this was 'favicon.ico' but i put a 'ProxyPass /favicon.ico !' in the mod_proxy section to pass this through to an empty favicon.ico file i created. So i must be something else. I will try and track down what the 500 error is. Any suggestions would be appreciated for the rewrite loop above. Thanks !

jdMorgan

3:32 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using a custom 403 error document, then you may need to exclude that URL-path from this rule to avoid the loop. The simplest way would be to modify the rule:

RewriteRule !^/error/HTTP_FORBIDDEN\.html$ - [F]

Also, since you're putting this code into specific VirtualHost containers and the port number is already declared, there is no reason to check for the 'correct' port numbers or "the other hostnames" inside each VirtualHost section. So the above could be simplifed to:

<VirtualHost 0.0.0.0:10025>
Options +FollowSymLinks -MultiViews
AcceptPathInfo off
RewriteEngine on
RewriteLog /tmp/rewrite.log
RewriteLogLevel 3
# If incorrect hostname for this port, return 403 unless the 403 errordocument itself is being requested
RewriteCond %{HTTP_HOST} !^uat2\.(3dns\.)?example\.com [NC]
RewriteRule !^/error/HTTP_FORBIDDEN\.html$ - [F]

Jim

Ralivo

4:11 pm on Dec 16, 2010 (gmt 0)

10+ Year Member



Thanks Jim that works perfectly ! I appreciate you sharing your expertise.