Rewrite INTO an alias directory

Background:
I’ve been tasked with merging three websites under one domain to help accumulate seo benefits. The three parts consist of an application, and two CMSs.

The desired structure is:
example.com/ –> app
example.com/c/pages/ –> cms 1
example.com/c/connect/ –> cms 2

So far so good, this is implemented and running fine. The /c/… have been implemented using Alias directives and all three applications are running in their self contained code-base directories.

The caveat in all this is that the root page for the domain i.e. ^example.com$ needs to display a landing page implemented in cms 1 (until an alternative landing page is constructed in the app). So this makes the structure:
example.com/ –> cms 1/
example.com/… –> app/…
example.com/c/pages/… –> cms 1/…
example.com/c/connect/… –> cms 2/…

At first I thought this was super simple and implemented the following on my dev machine:


AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST}        bikmo.com               [NC]
    RewriteCond %{REQUEST_FILENAME} ^/?$                    [NC]
    RewriteRule ^(.*)$              /c/pages/index.php      [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$              index.php?_url=/$1      [QSA,L]
</IfModule>

This works fine on my dev machine, but as soon as I put it up to production it fails (more on that shortly). As an interim measure, I’m redirecting within the app, but this means the url changes to /c/pages/ and I’d rather it didn’t.

I’ve checked the rewrite logs on the server and can see that the conditions are matching and it’s hitting the rewrite rule, which throws an internal redirect to /c/pages/index.php, however it seems to be coming back to the same .htaccess file in the same directory rather than hitting the Alias rule.

I’ve scoured the internet swatting up on mod_rewrite and I must say I feel I’m a lot more accomplished now that I was yesterday morning. In the course of my research I’ve found info regarding using END to stop the loop, PT to pass through to the next loop (where it’s been said that Alias would catch it), turned off MultiViews, adding RewriteBase within the conditional, and checking to see if it’s a subrequest using

RewriteCond %{IS_SUBREQ}  t

, all of which I have tested to no avail.

I’m now left scratching my head.

A couple of notes which may be helpful:

  • The site are organised in apps/{appname}/
  • The doc root of apache is set as app/{the-app}, rather than the traditional htdocs
  • The production server is running Ubuntu
  • Apache version is 2.4.7

Please can anyone point me in the right direction? Anyone?

Thanks in advance,
Jorj.

Where do the CMSs live, especially in relation to the app?

Thank you for the reply Jeff.

I’m running a server stack which we’ll display as root for the purposes of this:

  • Stack Root[LIST]
  • apache2[LIST]
  • htdocs
  • conf

[]apps[LIST]
[
]cms 1[LIST]
[]public (this is aliased as /c/pages)
[/LIST]
[
]cms 2[LIST]
[]public (this is aliased as /c/connect)
[/LIST]
[
]app[LIST]
[]src[LIST]
[
]public (apache conf points doc root to here)
[/LIST]
[/LIST]
[/LIST]
[/LIST]
[/LIST]

OK. Where does the htaccess live? It looks like it should live in <stack-root>/apps/src/public. If so, then you should be able to do this:

RewriteCond %{HTTP_HOST} bikmo.com [NC]
RewriteCond %{REQUEST_FILENAME} ^/?$ [NC]
RewriteRule ^(.*)$ …/…/cms1/index.php [L]

The reason is because rewrite rules actually operate on directory paths rather than URL paths.

Jeff, I can’t thank you enough.

In all the searching I did, nowhere was it put as clearly as you have just there, and I now feel a little silly for not inferring it from what I was looking at in the logs.

The .htaccess was indeed where you said so I set the path to go down a couple of levels, then back up into the cms and all is well in the world once more.

Ho Hum, actually it didn’t work at all.

I had quite forgotten overnight that I implemented an alternate fix (with it’s own pros and cons) which seemed to give the same behaviour.

As you can see from the logs below, the same subrequest is being fired in the same htaccess as it was originally. Interestingly, it looks like it’s adding a per-dir prefix (as expected) then stripping it back off again but leaving a forward slash on the beginning of the new path, meaning that it’s then trying to find /…/…/etc… instead of …/…/etc…


add per-dir prefix: ../../..//bikmo-wp-v1/bikmo/index.php -> /opt/bitnami/apps/cartel/src/public/../../../bikmo-wp-v1/bikmo/index.php
strip document_root prefix: /opt/bitnami/apps/cartel/src/public/../../..//bikmo-wp-v1/bikmo/index.php -> /../../../bikmo-wp-v1/bikmo/index.php
internal redirect with /../../../bikmo-wp-v1/bikmo/index.php [INTERNAL REDIRECT]

My thoughts are:
a) It’s regarding the document_root as THE root so it can’t back up out of it.
b) Maybe this could be achieved in the server config instead of .htaccess. I’ve seen somewhere that mod_rewrite works differently in these two places, however I’ve only got experience of using htaccess up to now. Would it go in the directory block? Or the vhost block? Or somewhere else?

Using a relative url:


[Thu May 22 14:27:39.370504 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:27:39.370548 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^' to uri ''
[Thu May 22 14:27:39.370569 2014] [rewrite:trace4] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='false' pattern='t' [NC] => not-matched
[Thu May 22 14:27:39.370580 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:27:39.370589 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern 'index.(php|html)' to uri ''
[Thu May 22 14:27:39.370598 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:27:39.370624 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '(.*)' to uri ''
[Thu May 22 14:27:39.370640 2014] [rewrite:trace4] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='bikmo.com' pattern='^search.bikmo.com$' [NC] => not-matched
[Thu May 22 14:27:39.370650 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:27:39.370658 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^(.*)$' to uri ''
[Thu May 22 14:27:39.370671 2014] [rewrite:trace4] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='bikmo.com' pattern='bikmo.com' [NC] => matched
[Thu May 22 14:27:39.370684 2014] [rewrite:trace4] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='/' pattern='^/?$' [NC] => matched
[Thu May 22 14:27:39.370693 2014] [rewrite:trace2] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] rewrite '' -> '../../../bikmo-wp-v1/bikmo/index.php'
[Thu May 22 14:27:39.370704 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] add per-dir prefix: ../../..//bikmo-wp-v1/bikmo/index.php -> /opt/bitnami/apps/cartel/src/public/../../../bikmo-wp-v1/bikmo/index.php
[Thu May 22 14:27:39.370718 2014] [rewrite:trace2] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip document_root prefix: /opt/bitnami/apps/cartel/src/public/../../..//bikmo-wp-v1/bikmo/index.php -> /../../../bikmo-wp-v1/bikmo/index.php
[Thu May 22 14:27:39.370728 2014] [rewrite:trace1] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#1570a10/initial] [perdir /opt/bitnami/apps/cartel/src/public/] internal redirect with /../../../bikmo-wp-v1/bikmo/index.php [INTERNAL REDIRECT]
[Thu May 22 14:27:39.370804 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/index.html -> index.html
[Thu May 22 14:27:39.370818 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^' to uri 'index.html'
[Thu May 22 14:27:39.370831 2014] [rewrite:trace4] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='true' pattern='t' [NC] => matched
[Thu May 22 14:27:39.370840 2014] [rewrite:trace1] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] pass through /opt/bitnami/apps/cartel/src/public/index.html
[Thu May 22 14:27:39.370909 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/index.php -> index.php
[Thu May 22 14:27:39.370923 2014] [rewrite:trace3] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^' to uri 'index.php'
[Thu May 22 14:27:39.370935 2014] [rewrite:trace4] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='true' pattern='t' [NC] => matched
[Thu May 22 14:27:39.370945 2014] [rewrite:trace1] [pid 4784] mod_rewrite.c(468): [client 70.32.40.2:51917] 70.32.40.2 - - [bikmo.com/sid#fc1200][rid#17043b0/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] pass through /opt/bitnami/apps/cartel/src/public/index.php

Using an absolute path:


[Thu May 22 14:24:39.482112 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:24:39.482165 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^' to uri ''
[Thu May 22 14:24:39.482255 2014] [rewrite:trace4] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='false' pattern='t' [NC] => not-matched
[Thu May 22 14:24:39.482281 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:24:39.482295 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern 'index.(php|html)' to uri ''
[Thu May 22 14:24:39.482306 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:24:39.482316 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '(.*)' to uri ''
[Thu May 22 14:24:39.482364 2014] [rewrite:trace4] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='bikmo.com' pattern='^search.bikmo.com$' [NC] => not-matched
[Thu May 22 14:24:39.482375 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/ ->
[Thu May 22 14:24:39.482385 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^(.*)$' to uri ''
[Thu May 22 14:24:39.482413 2014] [rewrite:trace4] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='bikmo.com' pattern='bikmo.com' [NC] => matched
[Thu May 22 14:24:39.482434 2014] [rewrite:trace4] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='/' pattern='^/?$' [NC] => matched
[Thu May 22 14:24:39.482444 2014] [rewrite:trace2] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] rewrite '' -> '/opt/bitnami/apps/bikmo-wp-v1/bikmo/index.php'
[Thu May 22 14:24:39.482460 2014] [rewrite:trace1] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1569910/initial] [perdir /opt/bitnami/apps/cartel/src/public/] internal redirect with /opt/bitnami/apps/bikmo-wp-v1/bikmo/index.php [INTERNAL REDIRECT]
[Thu May 22 14:24:39.482549 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/index.html -> index.html
[Thu May 22 14:24:39.482563 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^' to uri 'index.html'
[Thu May 22 14:24:39.482576 2014] [rewrite:trace4] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='true' pattern='t' [NC] => matched
[Thu May 22 14:24:39.482585 2014] [rewrite:trace1] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] pass through /opt/bitnami/apps/cartel/src/public/index.html
[Thu May 22 14:24:39.482625 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] strip per-dir prefix: /opt/bitnami/apps/cartel/src/public/index.php -> index.php
[Thu May 22 14:24:39.482638 2014] [rewrite:trace3] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] applying pattern '^' to uri 'index.php'
[Thu May 22 14:24:39.482650 2014] [rewrite:trace4] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] RewriteCond: input='true' pattern='t' [NC] => matched
[Thu May 22 14:24:39.482659 2014] [rewrite:trace1] [pid 4731] mod_rewrite.c(468): [client 69.59.28.19:55505] 69.59.28.19 - - [bikmo.com/sid#fc1200][rid#1571c40/subreq] [perdir /opt/bitnami/apps/cartel/src/public/] pass through /opt/bitnami/apps/cartel/src/public/index.php

Any reason you don’t just reverse proxy the paths rather than rewriting?