Using .htaccess file to redirect to a subdomain - apache

In my root folder, I have a home directory. I'd like all requests for mydomain.com or www.mydomain.com to be forwarded to home.mydomain.com. In my cPanel, I have already set up the sub-domain home.mydomain.com to point to mydomain.com/home.
Here's what I currently have in my .htaccess file:
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteRule ^(.*)$ \/home\/$1 [L]
This successfully forwards mydomain.com and www.mydomain.com to mydomain.com/home or www.mydomain.com/home, respectively. But home.mydomain.com/filename gives me an internal server error, instead of serving the file at mydomain.com/home/filename.

I'm not sure I understand the exact requirements, but it seems you want to rewrite all (www.)mydomain.com requests to the /home directory while your subdomain home.mydomain.com already points to that directory and thus should be exempt from that rewrite directive. If for some reason (www.)mydomain.com can't be set up to point to /home as well, you'd need a Rewrite Condition, something like
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /home/$1 [L]
I also removed the backslashes which should not be needed. You can use the htaccess tester to check rewrite rules easily online.

Related

Pointing subdomains to a "directory"

I have two questions regarding pointing subdomains to a directory:
Currently I run local, but I can run my site on a fake domain i have set up (with hosts file), its called mysite.com. How can i (by server settings?) do so All subdomains will point to / ? Example anders.mysite.com should show mysite.com and asdassdd.mysite.com also.
Maybe 1. is not necessary, but how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.
Why i thought of 1. is because I do not want to specify anywhere in the htaccess or any apache/domain setting, what the subdomain are, since this will be dynamic (created by logged in users in my webapplication)
Currently the users can use mysite.com/anders/ which is a URI they have created, and not a real directory. In the Kohana bootstrap I am then grabbing the URI and showing the relevant user page.
I am using Kohana MVC framework and have this in my htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Any help appreciated!
For 1, in your vhost just add a ServerAlias:
ServerAlias *.mysite.com
Then any subdomain (www included) will get pointed to the same document root.
If you want to put subdomains in their own directory (and since you have access to server config), you can use mod_vhost_alias and the VirtualDocumentRoot directive:
VirtualDocumentRoot /path/to/your/htdocs/%1
So if you request blah.mysite.com, you'll end up in /path/to/your/htdocs/blah as the doc root.
If it's a matter of you needing directory names of non-existent directories in the URI so that Kohana can route them, you'll need to make sure you have mod_proxy loaded:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,P]
To proxy the request back to Kohana with the right URI.
how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.
You can use this rule:
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
RewriteRule ^ /%1%{REQUEST_URI} [L]

Redirect wildcard subdomains to subdirectory, without changing URL in address bar

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
Here's what I have so far:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...
First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
Now, if you open asd.domain.com it should redirect you to domain.com/asd.
You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:
/
var/
www/
html/
.htaccess
subdomain1.domain.com/
subdomain2.domain.com/
subdomain3.domain.com/
Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.
Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.

Rewriting specific host to directories on localhost

I'm currently trying to redirect my local configured domains to its directories.. What is the best way to achieve this using htaccess?
Currently 'localhost' redirects to /Library/WebServer/Documents/ (mac OSX), i want to create an htaccess file in this directory that will lead all domains to its directory.
Example htaccess code of what I'm looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com/$
RewriteRule ^(.*)$ /Library/WebServer/Documents/example.com/$1 [L]
I hope someone can help me out, think there's an easy fix for this!
Kind regards,
This is not possible with .htaccess alone. You need access to the main server configuration file or at least modify the appropriate virtual host section.
If you can modify the main config file, you can add any number of virtual hosts.
If you're allowed to adjust your virtual host only, you must add as many ServerAlias entries to your virtual host as you have domains.
Then you can add the rewrite rules for the various domains
RewriteEngine On
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/example.com/$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/domain.com/$0 [L]
Try this
# get the server name from the Host: header
UseCanonicalName Off
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /var/www/vhosts/%0/httpdocs
<Location /var/www/vhosts>
AllowOverride All
Options +FollowSymLinks
</Location>
Then, you could try these examples:
example.com would show /var/www/vhosts/example.com/httpdocs
example2.com would show /var/www/vhosts/example2.com/httpdocs
Remember to add the domains to your hosts file.
Source

.htaccess redirect based on multiple conditions

Im trying to configure a .htaccess RewriteRule and i'm struggling.
I have a domain www.domain.com
I'm trying to add these conditions.
I'm working on www.domain.com/.htaccess
if URL is www.domain.com with no path to file, redirect to www.domain.co.uk
if URL is www.domain.com AND has a path to file, no rewrite, allow.
if URL is www.domain.com AND has a path to file AND file doesnt exist, return 404
if URL points to a directory, return access forbidden
They are my requirments.
I've tried multiple things. Heres my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
This code satisfies the first constraint.
However i have no idea how to check if a path exists, if the path is valid, or if its a directory.
A perfect answer would be code satisfying all the constraints and explainatoin how it works.
I've read the mod_rewrite documentation more times than I care to admit :'(
# Make sure / redirected to other domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/$ http://www.domain.co.uk [R=301,L]
# Make sure directory listings are forbidden
Options -Indexes
Note that you can use RedirectMatch, which is actually preferred over rewriting in this case
and there is no need to handle files at all in your case - if the URI is not /, then the rules do not apply and regular file handling will be performed (404 error if not found, etc... )

How to use mod_rewrite in a subdomain's .htaccess?

I have a domain (let the name be) mydomain.com.
I added a subdomain, sub.mydomain.com. (It's an other site, the two sites don't have anything to do with each other)
I have mapped the subdomain (with godaddy.com's tool) to /sub directory. It works well with static file paths, ex. http://sub.mydomain.com/js/script.js.php.
However, I would like to be able to use the rewrite module to get http://sub.mydomain.com/js/script.js
My .htaccess files:
.htaccess in the root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$
RewriteRule ^.*$ http://mydomain.com/sub%{REQUEST_URI} [L,P]
.htaccess in the /sub directory:
RewriteEngine on
RewriteRule ^js/script\.js$ js/script.js.php [L]
(I tried adding RewriteBase /sub/, I didn't succeed).
It seems as if Apache didn't notice the /sub's .htaccess
How can I get the rewrite work on the subdomain's paths?
SOLVED!
After a long, exhausting debugging and googling I found an Apache setting that made the trouble:
I added Options -MultiViews and voilá, it works!
/////////////////////////////////////////////
Now my configuration:
-no .htaccess in the root.
-.htaccess in the root/sub:
DirectoryIndex site/index.php
Options -MultiViews
RewriteEngine on
RewriteRule ^site/js/script.js$ /site/js/script.js.php [L]