tl;dr How can redirect http://test.lab.example.com/mech-platform/
(a non-existing folder) to http://test.lab.example.com/mech-platform-web/frontend/web/
(an existing folder), using .htaccess file in Apache, so that user still sees the original URL in browser, cannot access anything else on this server and that such redirect is not permanent (not cached by the browser)?
I have a test.lab.example.com domain which points to an IP address which points to htdocs
folder in my Apache installation. There I have an existing folder of mech-platform-web/frontend/web
, which is web-accessible entry folder for my application.
I want to use a "fake" (non-existing) folder or URL part within that domain:
http://test.lab.example.com/mech-platform/
to point to above mentioned folder:
http://test.lab.example.com/mech-platform-web/frontend/web/
All files in web
folder, all subfolders and sub-parts of URL as good as all query items must be rewritten and processed. No other folders or files, above this web-accessible folder, must be accessible at all.
The redirection or rewrite should be invisible to the end-user. So for example all calls to "fake" URL / folder:
http://test.lab.example.com/mech-platform/device/edit/1
http://test.lab.example.com/mech-platform/index.php?device=1&op=edit
Must be processed over the existing one:
http://test.lab.example.com/mech-platform-web/frontend/web/device/edit/1
http://test.lab.example.com/mech-platform-web/frontend/web/index.php?device=1&op=edit
The redirection or rewrite must be non-permanent. Meaning that the browser reading it must not cache it. If I remove .htaccess file from my web-root (http://test.lab.example.com/
) the redirection or rewrite must be stopped. And any browser accessing http://test.lab.example.com/mech-platform/
must hit the wall with 404 (after .htaccess folder removal) since this folder does not exists in reality.
###An attempt for a solution
I am a total newbie to Apache configuration or mod-rewrite, so all that I managed to achieve so far is to copy-paste modified solutions found in SO, adjust them and find them not working.
For example, I have tried this one:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/mech-platform/(.*)$ $1/mech-platform-web/frontend/web/$2 [R,L]
Which failed totally ("The requested URL was not found on this server").
I have also tried the following one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.\lab\.example\.com$
RewriteRule (.*) http://test.lab.example.com/mech-platform/$1 [R=301,L]
RewriteRule ^$ mech-platform-web/frontend/web [L]
This one fails with "Internal server error".
By a complete accident I have tried the above with "test" subdoman / part removed.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^lab\.example\.com$
RewriteRule (.*) http://lab.example.com/mech-platform/$1 [R=301,L]
RewriteRule ^$ mech-platform-web/frontend/web [L]
For the expected URL of http://lab.example.com/mech-platform/
it again throw "Internal server error". While when trying to access the subdomain as whole -- http://lab.example.com/
-- I am getting redirected to the correct subfolder of mech-platform-web/frontend/web
.
However this must be a product of some error / misunderstanding. And in addition it produces permanent redirection (browser caches it and reuses even if .htaccess file is removed from server). And it make it non-invisible. User can all the time see mech-platform-web/frontend/web
folder and even access folders above (like mech-platform-web/frontend
or mech-platform-web
. Which is total failure in terms of security.
Following rule should work for you in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.lab\.example\.com$ [NC]
RewriteRule ^mech-platform/(.*) mech-platform-web/frontend/web/$1 [NC,L]
If you want to block all other files and folders from visitors then add this rule in the end:
RewriteRule !^(mech-platform-web/|$) [NC,F]
This rule will do silent rewrite and will stop working as soon as you remove this .htaccess or comment out this rule.