1

I have this working rule:

RewriteRule ^(.*)/amp/$ https://example.com/$1?amp=1 [QSA,R=301,NC,NE,L]

For redirecting https://example.com/register/amp/ to https://example.com/register?amp=1.

And it is working well, but I would like that the URL in the browser didn't change so it will load the register?amp=1, but in browser still show /register/amp/.

To achieve this behaviour, I try modifying to:

RewriteRule ^(.*)/amp/$ ./app.php/$1?amp=1 [QSA,R=301,NC,NE,L]

But then it redirects to https://example.com/home/user/public_html/register?amp=1.

So it is adding the document root wrongly and I don't know how to modify the rule to achieve that.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
shakaran
  • 356
  • 1
  • 7
  • 19
  • Have you looked into "%{REQUEST_URI}"? check here too https://stackoverflow.com/questions/14070635/rewriting-url-to-hide-real-page-path – B. Shea Apr 30 '19 at 02:19

1 Answers1

1
RewriteRule ^(.*)/amp/$ ./app.php/$1?amp=1 [QSA,R=301,NC,NE,L]

You need to remove the R (external "redirect") flag. The ./ prefix on the substitution string is also superfluous here and just adds complexity that the OS must optimise out.

When you specify a relative path substitution in a directory context (.htaccess or <Directory> container in the server config) and you have not stated an alternative prefix (or "base" directory) with the RewriteBase directive, then Apache adds back the directory-prefix (ie. /home/user/public_html/ in this case) at the end of the rewriting process. It is because this is an external redirect that you are exposing this directory-prefix in a malformed redirect. You need an internal rewrite instead.

For example:

RewriteRule ^(.*)/amp/$ app.php/$1?amp=1 [QSA,NC,NE,L]

However, you've also included app.php in the substitution, which you've not shown in your target URLs? (app.php maybe correct - you should be rewriting to the actual filesystem path and not rely on any additional rewrites or subrequests by mod_dir.)

MrWhite
  • 12,647
  • 4
  • 29
  • 41