1

Apache's mod_userdir

Our company provides a service to host our webpage using the Apache's "per-user directory mod" (see doc).

That is, Alice, Bob, and myself can access our own subdirectory through

respectively.

Problem

The problem is that I have developed my website upon a root-based structure.

That is, in the dev environment, a resource can be accessed through http://example.com/resource, which should be translated to http://example.com/~me/resource in the prod environment.

For obvious reasons, this structure is preferable since it's easier to migrate to another webserver or port the code to other debugging environments (ex: IIS, Wampserver, ...). My initial plan was to write my code this way, and redirect traffic later in .htaccess.

Redirection through .htaccess

I've tried all the solutions listed here without any success.

I think the proposed solution in the above link is different from my issue, since .htaccess is at the root, and the redirection is done from root to subfolder.

In my case, .htaccess is in the subfolder. When a URL is clicked, it will redirect the client to http://example.com/resource, which is a request on the root. At this point, I cannot redirect traffic using my .htaccess since the client is now out of my subdirectory.

Here's what I've tried with my .htaccess.

Solution 1 (RedirectMatch):

RedirectMatch ^/$ /~me/

Outcome: http://example.com/~me is infinitely redirected to http://example.com/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me/~me.

Solution 2 (RewriteRule):

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/~me/
RewriteRule ^(.*)$ /~me/$1

Outcome: No redirection performed. Probably because my .htaccess becomes ineffective.

Question

Is there a way to redirect traffic to my subdirectory using my own .htaccess?

A very inelegant way is to redirect traffic from root, but this can disrupt traffic to Alice, Bob, and the admin.

Say, in the .htaccess of the root, all traffic from http://example.com/resource will be redirected to http://example.com/~me/resource. But if Alice, Bob, or the admin uses http://example.com/resource, they will be unhappy with my rule.

Is there any solution to this?

Edit: As pertinently pointed out by Stephen Ostermiller, one idea is to enforce redirection by setting up the base URL as per my environment. However, I'm asking if there would be any solution that does not involve rewriting my code and re-debugging all URL redirections. For example, by tweaking .htaccess or any superglobal config file would do.

chckx592
  • 61
  • 1
  • 9
  • Shouldn't you just change your code to know what the base href in dev in `http://example.com/` but the base href in prod is `http://example.com/~me/`? – Stephen Ostermiller Mar 29 '22 at 15:00
  • @Stephen Ostermiller It's a possibility, but I was looking for a solution that does not involve rewriting my code and re-debugging all URL redirections. An external config file, like .htaccess, is more convenient when working in other environments, I think. It's just better to have one code that is common to all webservers, and change a per-webserver config file, such as the include_path of PHP, redirections, etc. in .htaccess. I'm just asking if it's possible to tweak .htaccess to do what I want. – chckx592 Mar 29 '22 at 15:05
  • 1
    You don't usually want to rely on redirect to correct links in your web app, it is less error prone, performs better, and has better SEO to link to actual URLs. I usually recommend having a single configuration option for your webapp that lets you set the base URL if it needs to be deployed to a subdirectory. – Stephen Ostermiller Mar 29 '22 at 15:17
  • @Stephen Ostermiller Yes, makes sense, thanks. I just edited my question to see if there would be an alternative. If not, I think I'll choose your comment as answer. Thanks. – chckx592 Mar 29 '22 at 15:23
  • @Stephen Ostermiller I've accepted your answer and summarized it in my answer. Thanks. – chckx592 Mar 30 '22 at 10:16

1 Answers1

1

Thanks to Stephen Ostermiller for his comment, which I summarize here as the answer of my question.

We usually don't want to rely on redirects to correct links in our web app. Minimizing redirections is less error prone, performs better, and has better SEO to link to the actual URLs.

What I've done is to define an environment variable using SetEnv in .htaccess to specify the base URL. If it's not defined, then it returns false in my PHP code, but everything should work (you’re allowed to concatenate a boolean with a string). Otherwise, all my URLs substitute the root with the base URL if the environment variable is specified.

As Stephen Ostermiller suggested, it is recommended to have a single config option for your webapp that sets the base URL if it needs to be deployed to a subdirectory.

chckx592
  • 61
  • 1
  • 9