Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Rewrite URL to change Request URI and preserve it

Apache Rewrite

4:08 am on Mar 30, 2017 (gmt 0)

New User

joined:Mar 14, 2016
votes: 0

Hello All,
I am trying to work on Rewrite URL to achieve the following

End-user uses the URL https://www.actual.com/xy-test and I would need to put a rewrite rule to redirect them to another URL based on the Request URI (xy-test) , the actual end-point URI is https://www.application.com/ab-pro
In the entire request-response i will have to preserve the URL & URI so that the actual end user should never see https://www.application.com/ab-pro in his browser

I have tried the below

RewriteEngine On
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerName Off
SSLProxyCheckPeerCN Off
RequestHeader set X-Forwarded-Proto "https"
RewriteCond %{REQUEST_URI} ^/(xy-test|ab-pro)$
RewriteRule ^/(.*)$ https://www.actual.com/$1/ [R=301,L]
RewriteRule ^/xy-test/?(.*)$ https://www.application.com/ab-pro/$1 [L,P]
RedirectMatch ^/$ https://www.actual.com/xy-test

but the issue is once the request reaches the application ab-pro which is hosted on a tomcat container , Tomcat is doing a 302 redirect which i can see in the response header to the below

Location:https://www.actual.com/ab-pro and since ab-pro is not found on the apache it is throwing a 404 error

I was able to make it working by adding the below highlighted line , but with that approach user could notice the change in the Request URI from xy-test to ab-pro in the browser

RewriteCond %{REQUEST_URI} ^/(xy-test|ab-pro)$
RewriteRule ^/(.*)$ https://www.actual.com/$1/ [R=301,L]
RewriteRule ^/xy-test/?(.*)$ https://www.application.com/ab-pro/$1 [L,P]
RewriteRule ^/ab-pro/?(.*)$ https://www.application.com/ab-pro/$1 [L,P]
RedirectMatch ^/$ https://www.actual.com/xy-test

Can you please kindly suggest me on how to handle this

[edited by: phranque at 4:58 am (utc) on Mar 30, 2017]
[edit reason] unlinked urls for clarity [/edit]

5:22 am on Mar 30, 2017 (gmt 0)


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 195

a few details to start with:

- the [P] flag implies the [L] flag, so [L,P] is redundant - you only need to use [P].

Use of the [P] flag implies [L]

- you should never use the mod_alias directives (Redirect/RedirectMatch) when you are using mod_rewrite.
use an equivalent RewriteRule instead of RedirectMatch.

When not to use mod_rewrite:
https://httpd.apache.org/docs/current/rewrite/avoid.html [httpd.apache.org]
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file

Tomcat is doing a 302 redirect which i can see in the response header to the below

take a look at the ProxyPassReverse Directive:

This directive lets Apache httpd adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses. This is essential when Apache httpd is used as a reverse proxy (or gateway) to avoid bypassing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.

Using mod_rewrite for Proxying:
https://httpd.apache.org/docs/current/rewrite/proxy.html [httpd.apache.org]