Is there a reason the RewriteRule [P] flag doesn't seem to work on DreamHost?

208 Views Asked by At

I am working on a wordpress site hosted on DreamHost. There is a page on the wordpress site that has the stub 'profile' and uses a custom template that takes a URL parameter. An example of a URL is:

http://www.example.com/profile?profileSubject=subject1

I am writing a RewriteRule in the htaccess file so that I can type a url like this:

http://www.example.com/profile/subject1

My RewriteRule in the htaccess file looks like this:

RewriteRule ^profile/([^/]*)/? http://www.example.com/profile?profileSubject=$1 [P,L]

This works insofar as typing the /profile/subject1 URL will load the content correctly, but, the URL in the address bar changes to the profile?profileSubject=subject1 URL. I believe this is what the P flag(i.e. proxy) is supposed to be for, to stop the URL in the address bar from changing.

I happen to have a copy of the site I'm working on on my local server as well, so I tried putting the exact same line in the htaccess file for my local server, and there the P flag worked, and the URL in the address bar remained /profile/subject1 when the content was loaded.

So, my conclusion is that the P flag isn't working for some reason on Dreamhost, and I'm just wondering if anyone else has had a similar issue and/or if there's anything I can do here.

EDIT: Here is the requested result of logging a rewrite attempt:

[Fri Nov 25 14:37:29.213969 2022] [rewrite:trace3] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] add path info postfix: /Users/me/Sites/wordpress/profile -> /Users/me/Sites/wordpress/profile/javonte-green
[Fri Nov 25 14:37:29.214037 2022] [rewrite:trace3] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] strip per-dir prefix: /Users/me/Sites/wordpress/profile/javonte-green -> profile/javonte-green
[Fri Nov 25 14:37:29.214042 2022] [rewrite:trace3] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] applying pattern '^/?profile/(.+)$' to uri 'profile/javonte-green'
[Fri Nov 25 14:37:29.214050 2022] [rewrite:trace2] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] rewrite 'profile/javonte-green' -> '/profile?profileSubject=javonte-green'
[Fri Nov 25 14:37:29.214056 2022] [rewrite:trace3] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] split uri=/profile?profileSubject=javonte-green -> uri=/profile, args=profileSubject=javonte-green
[Fri Nov 25 14:37:29.214062 2022] [rewrite:trace2] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] trying to replace prefix /Users/me/Sites/wordpress/ with /~me/wordpress/
[Fri Nov 25 14:37:29.214067 2022] [rewrite:trace2] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] trying to replace context docroot /Users/me/Sites with context prefix /~me
[Fri Nov 25 14:37:29.214071 2022] [rewrite:trace1] [pid 1427] mod_rewrite.c(476): [client 127.0.0.1:60404] 127.0.0.1 - - [127.0.0.1/sid#7faf94036ef8][rid#7faf948656a0/initial] [perdir /Users/me/Sites/wordpress/] internal redirect with /profile [INTERNAL REDIRECT]

EDIT 2: I finally figured it out. The solution is to refer to the profile page via wordpress's index.php rather than the 'profile' stub:

RewriteEngine On
RewriteRule ^/?profile/(.+)$ /index.php?pagename=profile&profileSubject=$1 [QSA]

Done this way, content loads correctly without changing the visible URL.

1

There are 1 best solutions below

11
arkascha On

As @CBroe pointed out in the comment to the question the proxy module has to be enabled inside the http server for the P flag to be available. Though I fail to understand how the outcome you describe could occur with that rule in place. Maybe you have other, additional rules responsible for the redirection you observe?

Why do you want to use the proxy module for this? That means a full additional request each time. And it appears not necessary to me from your description.

Why don't you use a simple internal rewrite:

RewriteEngine on
RewriteRule ^/?profile/(.+)$ /profile?profileSubject=$1 [QSA,L]

That will keep the visible URL in the browser as https://www.example.com/profile/subject1 while internally calling the resource /profile?profileSubject=subject1, how ever that one is going to get resolved.