Welcome to WebmasterWorld Guest from 54.145.69.236

Forum Moderators: Ocean10000 & incrediBILL & phranque

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
posts:2
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)

Administrator

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

joined:Aug 10, 2004
posts:10859
votes: 67


a few details to start with:

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

https://httpd.apache.org/docs/current/rewrite/flags.html#flag_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:
https://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypassreverse

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]
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members