How to setup request proxy using URL rewriting

246 views Asked by At

I have an e-commerce site that resides in:

http://dev.gworks.mobi/

When a customer clicks on the signin link, the browser gets redirected to another domain, in order for authentication:

http://frock.gworks.mobi:8080/openam/XUI/#login/&goto=http%3A%2F%2Fdev.gworks.mobi%3A80%2Fcustomer%2Faccount%2Flogin%2Freferer%2FaHR0cDovL2Rldi5nd29ya3MubW9iaS8%2C%2F

I'm trying to rewrite http://dev.gworks.mobi/* to http://frock.gworks.mobi:8080/openam/*, without redirection.

I've tried this in the .htaccess of the dev.gworks.mobi site:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/openam(.*)$ [NC]
    RewriteRule ^(.*)$ http://frock.gworks.mobi:8080/$1 [P,L]
</IfModule>

But when I access http://dev.gworks.mobi/openam, it shows a 404 page not found page.

Can anyone help me to achieve my use case?

1

There are 1 answers

0
sepehr On BEST ANSWER

Try this:

RewriteEngine on
RewriteBase /

# Make sure it's not an actual file being accessed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Match the host
RewriteCond %{HTTP_HOST} ^dev\.gworks\.mobi

# Rewrite the request if it starts with "openam"
RewriteRule ^openam(.*)$ http://frock.gworks.mobi:8080/$1 [L,QSA]

This will rewrite all the requests to dev.gworks.mobi/openam to frock.gworks.mobi:8080.

If you want to mask the URI in a way that it's not visible to the visitor that she's visiting the authentication app, you need to add a P flag. Please note that it needs Apache's mod_proxy module in place:

RewriteRule ^openam(.*)$ http://frock.gworks.mobi:8080/$1 [P,L,QSA]

Feel free to drop the L flag, if it's not the last rewrite rule. See RewriteRule Flags for more information.

The 404

If it's all in place and you're still getting a 404 error, make sure that the target URL is not throwing 404 errors in the first place.

Second, check if you're still getting the error with the correct referrer URI set. It might be designed in a way to throw a 404, if the referrer is not correctly set. If that's the case, which I suspect, you need to use the R flag and redirect instead of proxying the request.

Last thing that comes to my mind, some webapps are not built in a way to figure out the URI address. The host, as well as the port number, might be hard-coded somewhere in the config files. Make sure that the authentication app is able to be run from another URL without the need to edit the configs.

Test

You can test the rewriterule online: