0

I'm hosting a few small client pages within my web application. Basically, they all are instances of the web application I wrote, for different clients. I'm deploying with capistrano, and so each time I deploy a new release, it actually gets deployed to a subdirectory called 'current' of the client's directory. So, for a client named client-name, it deploys to /var/www/deployments/client-name/current/ (Actually, this is a symlink to a release, but that's not important). My webapp requires that the "root" of the app points to the /main subdirectory.

Basically, my problem is this:

My root domain is client.example.com. What I want to do is be able to redirect a request typed into the browser such that http://client.example.com/client-name/something is actually pointing to http://client.example.com/client-name/current/main/something, where client-name is variable (there are too many for me to simply enumerate, although there is definitely less than 100). Ideally, this would be done without changing the location in the address bar.

I feel like this is a pretty simple use of Apache mod_rewrite, but I can't seem to find an example that does what I'm looking for. I would appreciate it if someone could help me out, because mod_rewrite makes my head hurt. ;)

jwir3
  • 155
  • 1
  • 2
  • 10

3 Answers3

2

Use the Redirect directive in mod_alias

Colt
  • 2,029
  • 6
  • 21
  • 27
  • Here is some more [Apache documentation](http://httpd.apache.org/docs/2.4/rewrite/avoid.html#redirect) addressing this very issue. Note, this is Apache's view of when NOT to use mod_rewrite! – Colt Apr 05 '16 at 02:01
  • I actually did read through that documentation. The problem I'm having is that I'm not sure how to specify the appropriate regex for the subdirectory to be variable. In other words, `client_name` could be named anything. – jwir3 Apr 05 '16 at 19:07
  • You don't need to do regex and, because "client_name could be named anything," you really CAN'T devise a regex to find "anything." You also noted that there is less than 100 of these. Just list each directive, one per line. It will be something like: `Redirect permanent "/client-name/current/" "/client-name/current/main/something/"` – Colt Apr 05 '16 at 19:50
  • I think perhaps I'm not being clear. I want to retain all of the URL containing `http://www.example.com/client-name`, append the two subdirectories `current/main`, and then append anything the client already added. So, for example, if the user went to `http://www.example.com/iamaclient/index.php`, it would alias to `http://www.example.com/iamaclient/current/main/index.php`. – jwir3 Apr 05 '16 at 20:28
  • Also, I failed to mention that these directories are being deployed by a script, so while there are < 100 right now, they have to be able to add/change without manual intervention of an admin changing the config. – jwir3 Apr 05 '16 at 20:29
0

{Comments not long enough to append, so had to go with a new answer to continue.}

You can do it two ways: (1) with the full URL, starting with http://..., which will handle the redirect by telling the client to hit the new URL, or (2) with a relative URL, which will "stay on" your server while handling the redirect: in the example you have given: the latter is implemented as:

 Redirect permanent "/iamaclient/" "/iamaclient/current/main/"

while in the former you would implement as:

 Redirect permanent "/iamaclient/" "http://www.example.com/iamaclient/current/main/"

In either case, everything after the trailing slash in the base link (i.e., the index.php or whatever the client types in the original URL) will be automatically appended after the trailing slash in the specified redirect path. This is what makes the Redirect directive attractive - it really is this easy!

As far as the script goes, I am not sure how to do this short of writing a script that generates a .config file. I note, however, that mod_rewrite does nothing to make this easier, only harder.

While I am at it, I should mention that EITHER mod_rewrite directovies or the Redirect directive will update the URL in the client browser.

Colt
  • 2,029
  • 6
  • 21
  • 27
  • IF all of the client names have some COMMON text pattern in them, it MAY be possible to write a mod_rewrite pattern to automate this. Details will be required, and you may need to hire someone to do it – Colt Apr 05 '16 at 21:59
0

So, I was actually able to get this to work with the following alias line:

AliasMatch "^/([a-zA-Z0-9]+)(/(.*))?$"   "/var/www/deployments/$1/current/main/$3"

I modified the user directory example given here to come up with what I wanted.

jwir3
  • 155
  • 1
  • 2
  • 10