Redirect subdomain to main domain - apache

I'm dealing with a site that supports both English and Spanish, and I'd like to know how to redirect all the requests to the language-specific subdomains to the main domain (en.mysite.com and es.mysite.com to mysite.com).
The whole site is programmed in PHP and it has a main script index.php that processes the language and section GET parameters and displays stuff accordingly.
Now, I tried to do the following in the .htaccess file in the root of mysite.com. I think it clarifies what I'm trying to do:
RewriteEngine On
# English redirects
RewriteRule ^en.mysite.com$ index.php?language=English&section=Main
RewriteRule ^en.mysite.com/store$ index.php?language=English&section=Store
RewriteRule ^en.mysite.com/create_account$ index.php?language=English&section=CreateAcc
# Spanish redirects
RewriteRule ^es.mysite.com$ index.php?language=Spanish&section=Feed
RewriteRule ^es.mysite.com/comprar$ index.php?language=Spanish&section=Store
RewriteRule ^es.mysite.com/crear_cuenta$ index.php?language=Spanish&section=CreateAcc
But this doesn't work. Can anyone tell me what I'm doing wrong here?

You can't include the hostname as part of the match in a RewriteRule, as it only matches against the URI. You'll need to use a separate RewriteCond that matches against the hostname for each of your rules:
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English&section=Main
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^store$ /index.php?language=English&section=Store
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^create_account$ /index.php?language=English&section=CreateAcc
Then the spanish rewrites:
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English&section=Feed
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^comprar$ /index.php?language=English&section=Store
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^crear_cuenta$ /index.php?language=English&section=CreateAcc

Related

mod_write htaccess with subdomains

I've been trying to figure out this htaccess issue and I can't seem to get it working and getting stuck on 500 errors.
I've looked around at multiple S.O. questions and general google but haven't found any that apply to my particular situation. Here it is:
I have example.com. I want if the user puts in: example.com or www.example.com to go to index.php.
But if the user puts in a subdomain subdomain.example.com; I want it to go to
dashboard.php?val=subdomain
Further more I'd like
subdomain.example.com/contact
to go to
dashboard.php?val=subdomain&page=contact
Here is what I have so far:
RewriteEngine On
RewriteBase /
# If doesn't start with www and it has a subdomain, redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule .? - [E=VAL:%1]
RewriteRule ^/?$ /dashboard.php?val=%{ENV:VAL}&page=idx
RewriteRule ^contact/?$ /dashboard.php?val=%{ENV:VAL}&page=contact [L]
#Note: I have tried this with and without the Env variables and only having 1 RewriteRule
# The "catch all other" redirect for images, files and if they do /cont by accident
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !dashboard\.php [NC]
RewriteRule . /dashboard.php?val=%1&page=idx - [L]
There may be subdomainA and subdomainB, etc so want to do a catch all on subdomains. Hopefully the question makes sense. My Apache knowledge is more limited and most of this is from googling around.
Give the following rules a try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^/?$ /dashboard.php?val=%1 [NC,L]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/dashboard\.php [NC]
RewriteRule ^.*$ /dashboard.php?val=%1&page=$0 [NC,L]

htaccess rewrite from a php file to subdomain

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

Properly redirecting a subdomain to subdirectory invisibly

I know similar things have been asked before, but none of the solutions I've found have seemed to work out. I'm by far an expert when it comes to mod_rewrite and it's ilk, so apologies if I'm missing something obvious.
I am trying to invisibly redirect subdomains to an index.php file in a subdirectory; this file takes the value of the subdomain as part of the query string, which is working fine.
The problem I'm having is that now everything in this subdirectory is being redirected to the index.php file, which I don't want to happen.
This is what I have thus far:
RewriteEngine On
RewriteBase /
# User dashboards
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,NC,QSA]
What I'm looking for is a situation where http://subdomain.example.com/ will lead to /app/index.php?user=subdomain, but http://subdomain.example.com/assets/stylesheet.css will go to /app/assets/stylesheet.css.
Thanks in advance!
If i understood well your example, you can do it this way:
redirects example.com to www.example.com to avoid having "empty" subdomain
internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
internally rewrites other things with "app" prefix
Which is represented by this code
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
EDIT: as you asked in below comments, here is how to manage also www subdomain
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally redirects www subdomain root to /site/index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?$ /site/index.php [L]
# internally rewrites every other root subdomains to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
Let add second rule to redirect assets to app/assets:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif)$ [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
or load all css/js/images directly from app:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*\.(css|js|png|jpg|gif)$ app/$0 [NC, QSA]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
EDIT: Sorry, I haven't tested it before, so there is working example:
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule !^app/assets/ app/index.php?user=%1 [L,QSA]

Folder to subdomain when autoappending www

We have a .htaccess-file like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mysite\.no [NC]
RewriteRule (.*) http://www.mysite.no/$1 [R=301,L]
Which autoappend www in front of the URL if it's missing. In addition we have to expand our site to a mobile version that we'd like to call m.mysite.no.
How do you do this and make the mod_rewrite ignore the www-rule? Unfortunately, this site is live, so we can't do much testing and we have little experience on the field.
Change:
RewriteCond %{HTTP_HOST} !^www\.mysite\.no [NC]
To:
RewriteCond %{HTTP_HOST} !^(www|m)\.mysite\.no [NC]