.htaccess: hiding subdomain with proxy redirect / rewrite - .htaccess

I need to access my site that sits in a subfolder from my domain.
The challenge - Due to my hosting structure, all sites are on the master domain's server in subfolders. Domains are mapped to those folders for each site. The master domain, however, is mapped to the server root like this -
• /www/ (root of www.masterdomain.com)
/folder 1/ (root of www.site1.com)
/folder 2/ (root of www.site2.com)
/folder 3/ (root of www.site3.com)
/folder 4/ (desired root of master domain)
If i install my CMS in folder 4 everything is fine, BUT the CMS code creates new assets in the server root (using DOCUMENT_ROOT i think), NOT in the subfolder.
Attempted fix 1:
Use a rewrite to remove subfolder and set all paths to include subfolder.
Failed - CMS acts as desired, but assets still created in the root.
Attempted fix 2:
Map a subdomain to the folder and rewrite URL to remove subdomain.
Failed: CMS works on subdomain and creates assets in the subfolder (win!), but can't rewrite a subdomain.
Attempted fix 3:
Map a subdomain to the folder, and use a proxy flag from the main domain to load content from it invisibly.
Progress made! Main homepage displays with the correct URL, however linked files and subfolders do not work.
So i can now reach my web app from:
app.exampledomain.com (app works perfectly on all fronts)
OR
exampledomain.com (proxy in effect, all subfolders and links broken)
Here's my htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Proxy subfolders
RewriteRule ^exampledomain.com/(.+?)?$ http://app.exampledomain.com/$1 [P,L,NC]
# Proxy main
RewriteRule ^(exampledomain.com)?$ http://app.exampledomain.com/ [P,L,NC]
So why are my subfolders / assets not working? Problem with the .htaccess? Bad logic?
Important notes -
Subfolder pages are called index.php so can be accessed from -
/folder/
The broken links on the page are in this format -
js/foundation.min.js
Thanks.

RewriteRule knows nothing about domain names, it works only with a local path.
Instead, check the domain name in the RewriteCond:
RewriteCond %{HTTP_HOST} ^(www\.)*exampledomain\.com$ [NC]
RewriteRule (.*) http://httpd.apache.org/$1 [P,L]
But using mod_alias or VirtualHost would be a more correct decision here.

Related

URL Rewrite all folders at root of subdomain to folder in a different file path

I need to rewrite from sub.domain.com/Xfolder/yfile.htm --> /local_path/all_customers/xfolder/yfile.htm
There are many xfolders in the all_customers folder. No folders or files exist in the IIS root of sub.domain.com. All of the folders and files actually exist in a local folder and not a URL.
I need something like this code, but I also need to allow for URL variables.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
RewriteRule !^file_path_to_folders\all_customers\$ [R=302,L]
URL rewrite module can only rewrite a URL to another URL so it is unavailable to rewrite URL to a physical path folder directly. You can create another website with the root folder /local_path/all_customers/. So that you can easily reverse proxy siteA/Xfolder/yfile.htm to siteB/Xfolder/yfile.htm.
Please install ARR and reverse proxy to achieve this.
ARR and reverse proxy
And reverse Proxy functionality is disabled by default, so you must begin by enabling it in Application Request Routing" feature.

htaccess rule to show my website under a specific path when accessing root domain URL

I have my website inside the following path on my remote web hosting server:
~/public_html/website/app
and need to find a way to show it when typing:
mydomain.com
so without the need of typing the full path:
mydomain.com/website/app/
One solution that came to my mind was to create a index.php file inside my root folder to automatically redirect to ./website/app/:
<?php
header('Location: ./website/app/');
exit;
but then I need to rewrite the URL in order to go from:
mydomain.com/website/app/
to:
mydomain.com
and I did not find a way to do that!
So, what do you think I should do to obtain that result and show my website under /website/app/ when accessing:
mydomain.com
and do not show the /website/app/ folder names?
Thanks a lot!
edit:
I was forgetting that I also have the website/server path so I need a rule that keeps valid the requests containing mydomain.com/website/server/.
Plus, I cannot use:
Options +FollowSymLinks
on my hosting (for security reasons), it's overridden by:
Options +SymLinksIfOwnerMatch
You have two options to do that.
If you just want to show the /website/app directory for your root url / then you can use DirectoryIndex directive .
DirectoryIndex website/app
This will show you the index file from /website/app folder if you visit the your root URI ie : example.com/ .
If you want to server files and directories from the /website/app instead of the / folder then you can use a RewriteRule that rewrites your root level requests to the subfolders .
RewriteEngine on
RewriteRule !website/app /website/app℅{REQUEST_URI} [L]

Setup website with / prefixed URLs to run in sub-directory

I am trying to host a website that is setup with links in the format /css/file.css and /js/file.js and I'd like to set it up in a sub-directory on an existing domain for demo purposes.
So
/demo/css/file.css is the true path
But the demo/index.php references
/css/file.css
I can have a custom .htaccess file within the sub-directory.
Is this possible without touching the main site on my domain?
If the request is for /css/file.css then rules have to be placed in root .htaccess not in /demo/ directory.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^((?:css|js)/.+)$ /demo/$1 [L,NC,R=302]

Redirect all subdomains to subdomain at different domain

Hi I need to redirect all subdomains in a domain to the same subdomain but at a different domain. The best way im guessing is through a htaccess file but im not sure how the file would be.
Example:
sd1.example.net ---> sd1.example.com
sd2.example.net ---> sd2.example.com
sd3.example.net ---> sd3.example.com
But I need this to be done for all of the subdomains in example.net. Thanks.
If you have an Apache server running on example.net and the requests for all the subdomains look in the same parent directory you can do something like the following:
RewriteEngine On
### Find the subdomain part (it will be available in %1)
### Use one of the RewriteCond-s and delete the other one
# Only redirect subdomains, not plain example.net
RewriteCond %{HTTP_HOST} ^(.*)\.example\.net$
## Redirect both subdomains and plain example.net (uncomment to enable)
#RewriteCond %{HTTP_HOST} ^(.*\.)?example\.net$
# Find the path requested (it will be available in $0)
# This rule does not attempt to match the domain, only the path
# Redirect subdomain and path to example.com
RewriteRule ^.*$ http://%1.example.com/$0 [L]
I haven't tested this so it might be missing query strings, etc. It will also undesirably redirect https:// to http://. As long as you have a single .htaccess file that can affect all your subdomains this should work, or at least be a very good starting point. Check out Apache's mod_rewrite documentation for more information about how this works.
EDIT
Having recently wanted to do exactly this myself recently, I have worked out a short .htaccess file that does the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com$
RewriteRule ^(.*?/)?public_html/(.*)?$ "http\:\/\/%1newdomain\.org\/$2" [R=301,NE,L]
It assumes the following file structure:
.htaccess
public_html/
+-content
lots/
+-public_html/
| +-content
of/
+-public_html/
| +-content
subdomains/
+-public_html/
+-content
My main site (newdomain.org) is in /public_html/. I have a number of subdomains, e.g. subdomains.newdomain.org which is in /subdomains/public_html/. This keeps all the files of each my subdomains completely separate from each other and my main site. (My hosting service recommends /public_html/, /public_html/subdomains/ but that means each subdomain is also accessible at newdomain.org/subdomains/ which is not what I want). The only restriction this gives me is that I can never have a subdomain called public_html, which I think you'll agree is perfectly acceptable.
The flags on the rule are as follows:
R=301 - Redirect with a 301 Moved Permanently code. You can change the code if you don't need a permanent redirect, e.g. 302.
NE - No Encoding - Don't URI encode the new address, i.e. keep % as %, not %25
L - Last - Stop processing rules
Note that the .htaccess file must be in the root directory of your web server, not in the directories with your content files. This is because the rewrite rule works at the file system level, not the URL address level.
An address:
any.subdomain.olddomain.com/any/address.html?any=query&you=like
is changed to:
any.subdomain.newdomain.org/any/address.html?any=query&you=like

Redirect URL from Virtual root subdomain to addon domain

hello there I have a fossis.org as my virtualroot so my addon domains are created inside the public_html directory.i have addon domain called www.techiestuffs.com which is in public_html/techiestufs directory.
Now my problem is it became two different version of the same site.You can see that by visitng www.techiestuffs.com and http://fossis.org/techiestuffs .So my entire site is became as duplicate contents and many URL from http://fossis.org/techiestuffs also indexed in search engine.
Both the website uses Drupal7 CMS.
Does anyone know how to redirect the later one to earlier one that is fossis.org/techiestuffs to techiestuffs.com .
How about something like this, for public_html/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^fossis.org$
RewriteRule techiestuffs(/.*)? http://www.techiestuffs.com$1 [R=301]

Resources