Mod Rewrite / Rewrite to Sub Directory - .htaccess

I've been trying to figure out mod_rewrite for days and I was wondering if you guys have any ideas
My primary domain is siteX.com, which maps to 'public_html' directory.
Recently, I added another domain, called siteY.com, and i marked 'public_html/siteY/public_html' as its root directory.
Now I'm trying to change my root directory for siteX, by mapping it to' public_html/siteX/public_html', but I when I implement mod_rewrite, it lets the users see the url to be 'http://www.siteX.com/siteX/public_html'. I need my primarly domain to map automatically to the correct directory with the using see only 'http://www.siteX.com'.
To better illustrate the directories im referring to.
public_html (siteX, my primary domain marks this as root directory)
- .htaccess file
- siteX
-public_html (siteX i want my primarily domain to mark this as root directory)
- siteY
-public_html ( siteY.com marks this as root directory)
I hope I made the problem clear. Thank you.
This is currently the rewrite rule I am using:
RewriteCond %{HTTP_HOST} ^(www.)?siteX.com [NC]
RewriteRule ^$ http://www.siteX.com/siteX/public_html/index.php
For my hosting company, I believe mod_rewrite is the only way to go

When you have http://www.siteX.com as part of the target in a RewriteRule, it automatically redirects the browser. It's like having the [R] flag. I think you want something like this:
RewriteCond %{HTTP_HOST} ^(www.)?siteX.com [NC]
RewriteCond %{REQUEST_URI} !^/siteX
RewriteRule ^(.*)$ /siteX/public_html/$1 [L]
To internally rewrite all requests for the host siteX.com to the /siteX/public_html/ directory.

Related

Need help in URL rewriting sub-domains

I recently registered a domain name kbcsurveyors.com. Then, I created two sub-domains, which created two new folders in the root folder.
My motive is that if I type kbcsurveyors.com/preinspection, it should point to preinspection.kbcsurveyors.com. Same for other sub domains.
In my .htaccess, which I placed inside root of mydomainname.com, I have written following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^preinspection/(.*)$   https://kbcsurveyors.com/$1 [L,NC,QSA]   # Handle requests for "Preinspection"
But this file structure does not work. How do I write the .htaccess file to achieve what I want?
Regards
EDIT
I have asked a fresh question as this one has been messed up. Here is the link:How to rewrite rules for sub-domains
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub-domain1.mydomainname.com
RewriteRule ^(.*)$ http://mydomainname.com/$1 [L,NC,QSA]
If you want to redirect requests for a subdirectory to the appropriate domain, you can use the rules you have (or similar), but need to specify the scheme http:// and may use the R|redirect flag
RewriteRule ^sub-domain1 http://sub-domain1.mydomainname.com [R,NC,L]
If you also want to forward the requested path, you must capture it and use in the target
RewriteRule ^sub-domain1/(.*)$ http://sub-domain1.mydomainname.com/$1 [R,NC,L]

Subdomain mod_rewrite (htaccess) to relative directory

I am having a bit of trouble rewriting sub-domains to directories that reside above the web root. I have had plenty of experience with mod_rewrite before, but this particular problem is beyond me.
From what I can tell, mod_rewrite is throwing a tantrum because I insist on using relative directories (..) to determine the directory in which the sub-domain files are located.
Unfortunately, there are two restrictions from my client's specifications:
Putting the sub-domain as a sub-directory of the web root is not an option. The sub-domain must not be accessible from anywhere except the specific sub-domain (there are likely to be directory clashes).
This means http://subdomain.example.com/ must not be accessible from http://example.com/subdomain/ as that directory may be used in the application on the root domain.
An absolute path to the sub-domain files is not known to the client as shared hosting will be used.
If anyone could help me with this problem it would be greatly appreciated! I'd love to start using this in future projects as well, it's quite an elegant solution compared to how we currently deal with sub-domains... If anyone can get it working that is!
Edit: Thought it might be useful to point out that on requesting http://subdomain.example.com/ a 400 Bad Request is returned, rather than a 500 Internal Server Error that I expected. Everything works as expected when requesting the root domain.
Current .htaccess file.
# Begin Rewrite Module for http://*.example.com/
# ==============================================
<IfModule mod_rewrite.c>
# Turn the rewrite engine on.
RewriteEngine On
RewriteBase /
# Map subdomains to their respective directories.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule (.*) /../public_subdomains/%1/$1 [L]
# Rewrite all requests for the root domain to always be served through "index.php".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
</IfModule>
Current directory structure.
/
application/
cgi-bin/
framework/
public_html
public/
css/
images/
js/
.htaccess
index.php
public_subdomains/
mysubdomain/
index.php
anothersubdomain/
index.php
How you implement this depends on how your host implements subdomains. Some simply map onto your DOCROOT. Others provide a control panel to allow you to do that subdomain -> subdomain docroot yourself. If (2) applies then what you want is already provided, so I'll assume (1) in this ans.
First point to note is that rewriting in an htaccess per-directory context is really URI mapping botched onto the DOCROOT hierarchy. ".." is not allowed and will throw a 500. In practice your are stuck within the domain's DOCROOT.
So public_domains must be a sub directory of DOCROOT -- in your case public_html.
However, you can still simply prevent any direct access to public_html/public_domains by a simple rule:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} /public_domains(/|$)
RewriteRule ^ - [F,L]
See my Tips for debugging .htaccess rewrite rules for more hints. You only want to barf on requests with public_domains on the entry pass. Also remember that you don't include the leading / and targets are relative to DOCROOT for a base of /.

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

htaccess and rewriting my URLs

I'm trying to organise my websites with .htaccess, and I want to following configuration:
The URL of the main site has to be beta.example.com/portal/
The other sites should be in beta.example.com/othersite/
If users go to beta.example.com, they should be redirected 301 to
beta.example.com/portal/
beta.example.com/portal (without the trailing slash) should redirect to beta.example.com/portal/
/portal/ is not a physical file path on the server. I want all these sites to
be in a folder together, say /html/www/websites/
The folder the portal resides in, should not be located in
/html/www/websites/portal/, but I want to be able to switch the live
site to a new version (in a different folder) on the server, quickly,
invisible, without any downtime. This goes for all sites by the way.
I'm currently working with two or three .htaccess files: one in the root (www.), which directs beta.example.com to the folder /websites/
#RewriteBase / #should I use rewritebase or not?
/RewriteCond %{HTTP_HOST} ^beta.example.com$
RewriteRule (.*) websites/$1 [L,NC]
A .htaccess file is in the folder /websites/, and maybe another one or maybe not in the folder a site resides in.
RewriteBase /websites # or, not...
# rewrite the active site to the folder /portal_v1.0/
RewriteCond %{REQUEST_URI} ^/portal/
RewriteRule (.*) portal_v1.0/$1 [L]
# rewrite the root to /portal/
RewriteRule ^$ http://beta.example.com/portal/ [L,NC]
However, I still can't get this to work exactly the way I want. One problem is what beta.example.com/portal redirects to www.example.com/websites/portal/ , I don't want that. Help! Thanks in advance!

Creating SubDomains to Absolute Paths with .htaccess

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Resources