1

Is there a way to redirect with mod_rewrite without changing the URL in the user's browser?

I saw a solution using [P] in the end of RewriteRule, but this is not working for me.

What I want:

https://my-server.com/propostas/billy.joe
   < ---> internally redirect to
https://my-server.com/subdir/propostas_usuarios/billy.joe

What I have:

  <LocationMatch "/propostas/(?<username>[^/]+)">
  RewriteEngine On
  RewriteRule ^/([^/]+)(.*) /subdir/propostas_usuarios/%{env:MATCH_USERNAME}
  </LocationMatch>

This is what is currently working. But after redirecting, I can see /subdir/propostas_usuarios in the new URL.

I have tried to use [P] like this:

  RewriteRule (.*) https://%{SERVER_NAME}/hpe/propostas_usuarios/%{env:MATCH_USERNAME} [P]

But this gives me this errors:

[Fri Dec 11 16:02:57.945091 2020] [proxy:debug] [pid 16725:tid 140351293593344] mod_proxy.c(1253): [client 10.0.105.36:52700] AH01143: Running scheme https handler (attempt 0)
[Fri Dec 11 16:02:57.945102 2020] [proxy_ajp:debug] [pid 16725:tid 140351293593344] mod_proxy_ajp.c(744): [client 10.0.105.36:52700] AH00894: declining URL https://my-server.com/subdir/propostas_usuarios/billy.joe
[Fri Dec 11 16:02:57.945124 2020] [proxy_fcgi:debug] [pid 16725:tid 140351293593344] mod_proxy_fcgi.c(1032): [client 10.0.105.36:52700] AH01076: url: https://my-server.com/subdir/propostas_usuarios/billy.joe proxyname: (null) proxyport: 0
[Fri Dec 11 16:02:57.945131 2020] [proxy_fcgi:debug] [pid 16725:tid 140351293593344] mod_proxy_fcgi.c(1035): [client 10.0.105.36:52700] AH01077: declining URL https://my-server.com/subdir/propostas_usuarios/billy.joe
[Fri Dec 11 16:02:57.945149 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2338): AH00942: HTTPS: has acquired connection for (*)
[Fri Dec 11 16:02:57.945159 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2393): [client 10.0.105.36:52700] AH00944: connecting https://my-server.com/subdir/propostas_usuarios/billy.joe to my-server.com:443
[Fri Dec 11 16:02:57.946130 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2616): [client 10.0.105.36:52700] AH00947: connected /subdir/propostas_usuarios/billy.joe to my-server.com:443
[Fri Dec 11 16:02:57.946210 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(3085): AH02824: HTTPS: connection established with 10.30.6.52:443 (*)
[Fri Dec 11 16:02:57.946233 2020] [proxy:error] [pid 16725:tid 140351293593344] AH00961: HTTPS: failed to enable ssl support for 10.30.6.52:443 (my-server.com)
[Fri Dec 11 16:02:57.946236 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2353): AH00943: HTTPS: has released connection for (*)

Any ideas?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
deejah
  • 33
  • 4
  • Where exactly are you using this directive? In a _server_ or _virtualhost_ context? "But after redirecting, I can see `/subdir/propostas_usuarios`" - what type of "redirect" was this? 302? 301? – MrWhite Dec 11 '20 at 22:47

1 Answers1

0

The P flag sends the request through mod_proxy - which does not appear to be required here. You simply require an internal rewrite to another subdirectory.

<LocationMatch "/propostas/(?<username>[^/]+)">
RewriteEngine On
RewriteRule ^/([^/]+)(.*) /subdir/propostas_usuarios/%{env:MATCH_USERNAME}
</LocationMatch>

This looks like it should work - there is no external redirect here, as you appear to be observing. However, there is no L (oe END) flag on the RewriteRule directive, so processing will continue and it may be a later directive that triggers the redirect? (Although external redirects should really be before any internal rewrites.)

If you also have a .htaccess file (or <Directory> container) then these directives are also processed later and could potentially trigger a redirect. (?)

This rule can also be "simplified" - there's no need for the <LocationMatch> wrapper, as it can all be done in a single RewriteRule directive.

For example:

RewriteEngine On
RewriteRule ^/propostas/([^/]+)$ /subdir/propostas_usuarios/$1 [L]

I've put an end-of-string anchor on the regex, otherwise it would match a URL of the form /propostas/billy.joe/anything. The $1 is a backreference to the first captured group (ie. the username) in the RewriteRule pattern.

As mentioned above, I've included the L flag to prevent further directives in the current context being processed. However, directives in <Directory> containers (and .htaccess) will still be processed.

If you were previously seeing an external redirect then you will need to esnure your browser cache has been cleared before testing.

MrWhite
  • 12,647
  • 4
  • 29
  • 41