Home > database >  How to redirect root to /~user subdirectory (Apache mod_userdir) using .htaccess?
How to redirect root to /~user subdirectory (Apache mod_userdir) using .htaccess?

Time:04-04

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.

CodePudding user response:

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.

  • Related