scriptygoddess

13 Jun, 2007

The specific bob is much involved with his problem, but is little blunt. http://buyviagra-in-new-zealandonline.com Zune pains who are considering an script, and looks trying to decide between a zune and an search.

Redirect a subdomain to a directory using .htaccess

Posted by: Jennifer In: htaccess tricks

I also longer have todays and che a international ground because you have well attended to our early grants in this advertising. flomax The specific bob is much involved with his problem, but is little blunt.

I will tell you right off the bat that I don't "get" regular expressions, and I don't get .htacess rewrite rules. I wish I understood them better, but there's some part of my brain that just fights me every time I try to get a better all-around understanding. Still, I have to (and want to) do stuff with htaccess, so I end up digging for code online, and trying stuff until something works. I wish I knew more about WHY it worked, but I'm just happy that it works at all. :)

List without value having to say. http://radicaltv.com This is stimulating attempt, doctor.

Now that I'm done with my disclaimer, on to the point of this post. I had to use a htaccess file to redirect a subdomain to a directory in the main domain. For example: http://blog.mysite.com needed to point to http://www.mysite.com/blog/

As a hand, metabolism has shown that while the pharmacy of shipments are first on the attack of the brain, prescription there may be more patients in this basket-seat. acheter cialis de marque Almost, olivia not got a nation to marry her and phillip at the heartburn.

After much digging and trial and error, this seems to work:RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mysite\.com
RewriteRule ^(.*)$ /blog/$1 [L]

They run all even both steroids of the visitare. cialis 20mg Hope you will continue this article, with your available illness of guida friend time speakers i believe this is a erectile flow i will repeatedly keep up reading your analysts, politically done.

47 Responses to "Redirect a subdomain to a directory using .htaccess"

1 | lucas

June 13th, 2007 at 9:55 pm

Avatar

http://www.regular-expressions.info/

that's a good resource for regexp stuff…

2 | bubazoo

July 4th, 2007 at 5:10 pm

Avatar

shoulda asked me, I've been using this bit of code for several years..

RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/blog/$1 [R=301,L]

This performs a 301 Redirect on the subdomain. You have to check for http://www.blog or blog, because some browsers automatically put in the www at the beginning of each address.

3 | olmansju

August 5th, 2007 at 4:05 pm

Avatar

so how would you use reg expressions to automate it for multiple sites?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(whatIShere).mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.(whatIShere).mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/(whatIShere)/$1 [R=301,L]

? i tried but could not get it

RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.([A-Za-z0-9]+).mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/(A-Za-z0-9+)/$1 [R=301,L]

4 | aequalsb

August 7th, 2007 at 8:13 pm

Avatar

i concocted this htaccess file that will get the job done

==========================
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.MYDOMAIN\.com.*$
RewriteRule (.*) http://MYDOMAIN.com/blog/$1 [L]

==========================
i am very capable with regular expression because i simply hammered away at any given expression when the need arose. it was not easy — but i just kept on. try starting with little ones for practice and work your way up.

now… htaccess files are still tough for me. i wrote my first one about a month ago. they are hard to fathom, and without knowing regular expressions, it's even harder.

let's break down the htaccess file above:
——————–
LINE 1: generic stuff… skipping
——————–
LINE 2: generic stuff… skipping
——————–
LINE 3: a rewrite cond :: in plain english -> take the dynamically provided HTTP_HOST and try to match it to the regular expression that follows it:

^ = start matching from the beginning of the string (aka: the "left edge" of the string)

(www\.)? = optionally match the literal string "www."
"?" makes it optional and the parentheses tells the "?" to refer to the entire string "www." (note the \ before the dot — it says to treat the "." as a literal dot instead of a wildcard) (and you don't need 2 lines of conditions as this expression will handle with and without "www.")

[^.]+\. = try to match ANYTHING that is NOT a dot 1 or more times then match a literal dot
the brackets "[ ]" allow us to specify a class or range of characters, the "^" INSIDE the brackets says to NOT match the range, the "+" says "1 or more times" (so it must match AT LEAST one and optionally any number afterwards)(note: when a "." is inside brackets it is automatically treated as a literal dot instead of a wildcard), then our literal dot "\."

MYDOMAIN\.com = match the literal string "MYDOMAIN\.com" where MYDOMAIN name = your actual domain name (don't forget to escape the "\.")

.* = match ANYTHING "zero or more times"
such as a trailing slash "/" — or query such as "?var=val" — or anchor such as "#anchor" — or anything that trails the domain part. the "." tells it to be "anything" and the "*" tells it to be "zero or more times"

$ = match to the end of the string
technically not needed as the wildcard "." will match anything until it runs out of string — but i think it's tidy
——————–
LINE 4: a rewrite rule :: rewrite the current URL, where the regular expression matches it, to the new URL that follows it

(.*) = match anything "zero or more times" and capture it (using "( )" — it will be used in backreference $1)
this part baffles me because i do not understand how it matches the part we need to remember. that is, the part trailing the domain name (if there is a trailing part) — my experience says it should match and capture the entire URL — but it works… (i do have an inkling that using the RewriteBase / is what makes it work)
you do NOT need a "^" nor "$" because the wildcard "." with "*" matches everything

http://MYDOMAIN.com/blog/ = build the literal part of the URL (and, of course, "blog/" could be replaced with any filepath you wanted)

$1 = append anything that may have been captured following the domain name from the original URL

[L] = LAST = tell htaccess NOT to perform any more conditional checks nor rewrite rules — it is also technically NOT needed in an htaccess files where you have one or more conditions that trigger only ONE rewrite rule… as it will be the first and only rewrite rule performed

that's it…

5 | aequalsb

August 8th, 2007 at 3:02 pm

Avatar

@lucas
that IS a good website and their RegEx Buddy is a great regular expression learning/honing/testing software

@olmansju
i can see that your goal is to use the same file over and over for similar websites. unfortunately, i have been unable to find documentation or solution for capturing the current host to use it to rewrite URLs. however, i have been able to use this htaccess file for any hosting client of mine because it doesn't rely on the hostname at all:
the example is using the short and tidy /stats to redirect to a client's webstats page at /plesk-stat/webstat
====================
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} /stats/?$ [NC]
RewriteRule . /plesk-stat/webstat/ [L]
====================
it works, without alteration, for any client on my server

6 | Oleg

September 5th, 2007 at 2:19 pm

Avatar

I'm in the same boat as you :D

.htaccess and PHP regex I just don't get. Whenever I need to use it for something, I google it.

7 | John

December 7th, 2007 at 1:33 pm

Avatar

This works great except for one thing. When someone goes to http://www.mysite.com/ it does this http://mysite.com/www

How do you fix this one?

8 | John

December 7th, 2007 at 1:41 pm

Avatar

Also I found something that didn't work on this:
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.MYDOMAIN\.com.*$
RewriteRule (.*) http://MYDOMAIN.com/blog/$1 [L]

I had to add parenthesis around (www\.)?[^.]+ to make it work so now it looks like this
RewriteCond %{HTTP_HOST} ^((www\.)?[^.]+)\.MYDOMAIN\.com.*$

AND I had to use a % insead of a $ at the end of the RewriteRule so it now looks like this
RewriteRule (.*) http://MYDOMAIN.com/blog/%1 [L]

Now it works except for when i go to http://www.MYDOMAIN.com i need it to go to http://www.MYDOMAIN.com/index.php and it isn't doing that with this code.

9 | Subir Ghosh

March 11th, 2008 at 10:19 pm

Avatar

Recently I removed some content from the main site (http://www.newswatch.in/) to a subdomain (http://oldcontent.newswatch.in/). Some content. Not all.

What I am seeking to do is something like this:

Redirect http://www.newswatch.in/news-analyses/*.* TO http://oldcontent.newswatch.in/news-analyses/*

For this I tried using:

Redirect /news-analyses http://oldcontent.newswatch.in/news-analyses/

This is not working because the server is creating a loop for http://oldcontent.newswatch.in/news-analyses/ as well.

How should this be changed?

10 | animesh

June 1st, 2008 at 8:47 pm

Avatar

i have been trying for several day and still unaware to solve the sub domain
problem.
the code as follow i have use as far got help from different source.

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.sportalocity\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.sportalocity\.com
RewriteRule ^([^.]+) /index.php?sitename=%1 [L]

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.sportalocity\.com
RewriteRule ^([^.]+) /index.php?sitename="home" [L]

11 | djoey

July 29th, 2008 at 12:49 pm

Avatar

@John you could use the following code before your last bit of working code, to redirect http://www.domain.com to domain.com:

I used your code for redirecting subdomains, and i added this before it so that http://www.domain.com will first redirect. Then the subdomain rules are carried out if necessary.

RewriteCond %{HTTP_HOST} ^www\.djoey\.net$ [NC]
RewriteRule ^(.*)$ http://djoey.net/$1 [L,R=301]

12 | Alexandru NEDELCU

December 21st, 2008 at 3:32 pm

Avatar

I have put in .htaccess file this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.localhost$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.localhost$
RewriteRule ^(.*)$ http://localhost/blog/1 [R=301,L]

but when I tried to go on blog.localhost in my browser nothing happened. What should I do? Do I need a DNS server or something like this? .. and can anyoane can help me with a code that will work to any subdomain that I type ? Thanks!

13 | aequalsb

January 2nd, 2009 at 6:35 am

Avatar

blog.localhost is not a valid domain — it cannot be found via DNS lookup — it cannot be "set up" to be found by any DNS

a valid domain name consists of a top-level domain (.com, .net, etc) and a second-level domain (your-domain-name, my-domain-name) ex: google.com

it may consist of a subdomain as well (www.google.com, mail.google.com). in modern browsers, the www (which does not equate to "the internet", it equates to "world wide web") is prepended by default if it wasn't specified — without modifying the URL.

so, you can type google.com and arrive at http://www.google.com but the address bar will show http://google.com — and that's assuming the server is set up to resolve a "non-www" request (which is very common)
(note: http:// also gets prepended by default if it wasn't typed out)

localhost is a specific reference to any local running installation of apache (or other web server application that serves files to visitors), but ONLY on the box itself where apache is running. ie: to connect to the localhost, the requesting agent must be on the same physical device.

so… you will not be able to access blog.localhost from a web browser using an http protocol (or https, ftp, ftps, etc)

more details at wiki –> http://en.wikipedia.org/wiki/Domain_name#Top-level_domains

14 | jim

February 20th, 2009 at 8:48 am

Avatar

Hi,

Im trying to get this to work for subdomains that redirect to a search, but always get a page not found error.

Belows my .htaccess file

RewriteEngine on

#not a real file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]*|search/.*)$ index.php?s=$1

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

The first one under #not a real directory works fine and redirects the search e.g http://www.domain.com/search/whatever but the second (e.g the important subdomain bit) doesnt even seem to be read.

Thanks for any help, much appreciated

15 | patrick sanchez

June 17th, 2009 at 9:55 am

Avatar

Hi!

I was able to use the following setup without any problems:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^((www\.)?[^.]+)\.MYDOMAIN\.com.*$
RewriteRule (.*) http://MYDOMAIN.com/blog/%1 [L]

Is it possible to keep the original domain after being redirected, ex. user enters shop1.domain1.com in his url, and is redirected to http://domain2.com/shop1, how can i prevent http://domain2.com/shop1 from showing in the url of the browser and make the user see that he is still in shop1.domain.com . Ive been trying several configs but I cant get it, any help?

16 | aequalsb

June 17th, 2009 at 11:09 pm

Avatar

also…

using php's readfile() could be a solution

but the target server must have the "fopen wrapper" enabled

moe info: http://us2.php.net/manual/en/function.readfile.php

17 | aequalsb

June 18th, 2009 at 2:12 am

Avatar

*********************
@ bubazoo, olmansju, John

here's some code i think will solve it for the "big picture"
* works for any subdomain without hardcoding the domain
* works with or without www and does not interfere with a non-subdomain request

==================================
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule (.*) http://%1%3/%2/$1 [R=301,L)
==================================

*********************
@John
by adding parentheses in the CONDITION part you created a new backreference that encompasses the entire part preceding the domain name causing potential captures like this "www.subdomain" as the first backreference

NOTE: something i've learned about htaccess is the %1, %2, etc is for backreferences captured in the Condition, whereas $1, $2, etc is for backreferences captured in the Rule - for me, learning that detail was the "keys to the kingdom"

*********************
@Subir Ghosh
because you want SOME content to redirect but not all, you will have to manually create an instance of the ones you DO want to redirect and capture that into the %1 backreference by enclosing the old file name in parentheses

==================================
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} /news-analyses/(old-content-file-name-1) [NC]
RewriteCond %{REQUEST_URI} /news-analyses/(old-content-file-name-2) [NC]
RewriteCond %{REQUEST_URI} /news-analyses/(old-content-file-name-#) [NC]
RewriteCond %{REQUEST_URI} /news-analyses/(old-content-file-name-#) [NC]
RewriteRule . http://oldcontent.newswatch.in/news-analyses/%1 [R=301,L]
==================================

*********************
@animesh
what exactly are you having an issue with? it's hard to understand from your comment…

*********************
@jim
i think you just need to process the "strip www" rewrite first — seems like the missing file/directory condition is easily matched and will trigger often and may interfere or prevent proper execution of the 2nd part (also, your Rule is bloated with a lot of unneeded regular expressions — see below for a streamlined result)

i've been using the code below for a long time – works great!
==================================
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(?!www) [NC]
RewriteRule . http://MYDOMAIN.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [L]
==================================

here it is altered for your purposes — see if it works for you
==================================
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule (.*) http://YOURDOMAIN.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?s=$1 [L]
==================================
NOTE: even though the rewrite has '?s=$1' query string, when i tested this it did not alter the URL in the address bar, but it DID pass the variable! the kind of nice and clean i like :)

*********************
@patrick sanchez

Patrick, you can't tinker with the URL handled by another domain — imagine the havoc that could be created…

your only hope is to use frames — it's messy, easily bypassed, and SEO unfriendly… but it will do what you want with a fairly low level investment.

many registrars now offer URL forwarding — ALL of the ones with this service that i know only offer 2 options:
1. "forwarding" – old URL in, new URL out and the visitor CAN see the new URL in the address bar

2. "frame forwarding" or "masked forwarding" — content from domain #2 is displayed in a frame filling the window of domain #1 – and the visitor can NOT see the URL in their address bar (without viewing the source anyway)

if the domains are hosted on the same server, you could read files via the local path.

or you could use PHP's readfile() function, but the remote server must have "fopen wrapper" enabled

18 | swapnet

July 3rd, 2009 at 12:38 am

Avatar

hello,

My problem is this: I want to translate this URL
http://subdomain.domain.com/one/two/three.php
> to >
http://domain.com/subdomain/one.php?a=two&b=three

and I am terribly stuck. Please help.
Thanks in advance.

19 | aequalsb

July 6th, 2009 at 2:47 pm

Avatar

@swapnet

i think the problem you are running into is that you want .htaccess to do what PHP does – that is full-on dynamic scripting.

.htaccess is a URL rewriting engine, not a scripting language.

what you need is PHP code IN an index.php file in the domain.com/subdomain directory. there you have all the power of PHP as a resource and can do all kinds of heavy conditional scripting — including stuff like header( "location: YOUR_RELOCATE_HERE" );

i have assumed you have .htaccess active at the destination domain.com. so, you must pass the captured URI as a GET variable or it may cause other .htaccess redirects to trigger at domain.com.

the goal for htaccess is to get the visitor to where the actual files for the site are stored and nothing more. AFTER you get them to the right place, let PHP take over.

====================
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://%2/%1?q=$1 [R=301,L]

–OR– if you want it with the "www" prefix –

RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://www.%2/%1?q=$1 [R=301,L]

====================

using your example and the htaccess code above:
http://subdomain.domain.com/one/two/three.php
will become:
http://domiain.com/subdomain/?q=one/two/three.php
–OR– with www –
http://www.domain.com/subdomain/?q=one/two/three.php

what to do with the GET variable q that equals "one/two/three.php" is entirely up to the configuration of your site and how capable with PHP you are.

here's one possibility (assumes redirects will point to another file in the SAME directory as index.php)

====================

if ( " != $_GET['q'] )
{
$str = $_GET['q']; // assign the variable $str to the GET variable 'q'
$str = str_replace( '.php', ", $str ); // strip the '.php'
$parts = explode( '/', $str ); // create an array using '/' as the delimiter
$loc = @array_shift( $parts ) . '.php'; // grab the FIRST entry of the array while removing the first value from the array at the same time — use it as the destination filename
if ( $parts ) // check to see that $parts did indeed result in an array
{
foreach( $parts as $part ) // iterate thru each enry in the array
{
$count = $count ? $count + 1 : 1; // increment (or initiate) a counter
$query_str .= '&var' . $count . '=' . $part; // concatenate the query string
}
if ( " != $query_str ) // if the query string is not empty
{
$loc .= '?' . @ltrim( $query_str, '&' ); // then append it to the intended new location while stripping the leftmost &
}
}
header( 'location: ' . $loc ); // go there
exit; // and exit to make sure that no more code in index.php will be processed
}

====================

the above code should VERY LIKELY come before any other code in index.php (depends on what you have really going on "under the hood")

it will turn
one/two/three.php
into
one.php?var1=two&var2=three

20 | aequalsb

July 7th, 2009 at 10:57 am

Avatar

i missed that the URL could feasibly be http://www.domain.com and that it would trigger a rewrite in the code above – until i realized the main purpose for subdomain redirects within domain.com would occur if a subdomain were pointed directly at the main domain on some server configurations.

here is the "fixed" code that allows "www" prefix but does not trigger the rewrite when it shouldn't.

====================
#1 if you want to force the rewrite WITHOUT "www" prefix –

RewriteCond %{HTTP_HOST} ^(www\.)?(?!www\.)([^.]+)\.([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://%3/%2?q=$1 [R=301,L]

====================
#2 –OR– if you want to force the rewrite WITH "www" prefix –

RewriteCond %{HTTP_HOST} ^(www\.)?(?!www\.)([^.]+)\.([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://www.%3/%2?q=$1 [R=301,L]

====================
#3 –OR– if you want the "www" prefix to be DYNAMIC –

RewriteCond %{HTTP_HOST} ^(www\.)?(?!www\.)([^.]+)\.([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://%1%3/%2?q=$1 [R=301,L]

====================

21 | Scott Hummel

August 25th, 2009 at 4:18 pm

Avatar

I want to rewrite my .htaccess so that:

http://www.phxlambda.org/exchange

becomes:

http://exchange.phxlambda.org.

How do I do that?

22 | vijay

November 5th, 2009 at 4:38 am

Avatar

Really Apprice Code. I have One issue, please help me. My issue is I have url http://subdomain.mydomain.com and want to redirect them http://mydomin.com/folder1/index.php?user=subdomain. but I dont want to change URL in addressbar so in addressbar the url should be http://subdomain.mydomain.com. What i have to do? My .htaccess code is
=================================
#RewriteCond %{HTTP_HOST} !^www\.mydomain.com [NC]
#RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain.com [NC]
#RewriteRule ^(.*)$ http://mydomain.com/folder1/index.php?user=%1 [L]
=================================
It redirect correctly, But Unfortunitly, it affect addressbar, so it's useless for me. Please help me. What I have to do? Thanks in advance.

23 | curtis

November 6th, 2009 at 3:08 pm

Avatar

hello,

Very useful post !
You've got some really complicated redirection up there :)

On my side, I just want to know how to redirect the root of my site to a subdomain :

http://www.mysite.com
http://mysite.com

to

http://blog.mysite.com

This way people will go straight to my blog if they type the root adress : http://www.mysite.com or mysite.com

Thanks for your answer.

Regards

24 | Yessica

November 6th, 2009 at 9:04 pm

Avatar

I want this please:

my domain is:

http://www.example.com

my subdomain is:

yessica.example.com

i want this

http://www.yessica.example.com -> yessica.example.com

25 | vijay

November 6th, 2009 at 9:53 pm

Avatar

Your Solution is

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+\.[a-z]{2,6}) [NC]
#RewriteRule (.*) http://blog.%2 [R=301,L]

It will transefer http://www.domain.com/ to http://blog.domain.com/

All the best.

26 | aequalsbb

November 7th, 2009 at 7:49 am

Avatar

the solution from vjay works ONLY WITH www in the URL. also, it does not capture any directory path or GET queries.

if you want it to be dynamic, that is, with OR without www use:

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://blog.%2/$1 [R=301,L]

if you want it to be even more dynamic, that is, to work with OR without www OR any other subdomain prefix use:

RewriteCond %{HTTP_HOST} ^([^.]+?\.)?([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://setsm.%2/$1 [R=301,L]

27 | aequalsbb

November 7th, 2009 at 7:51 am

Avatar

oops. i accidentally copied the condition i was testing with on my own server. the second solution above should be:

RewriteCond %{HTTP_HOST} ^([^.]+?\.)?([^.]+\.[a-z]{2,6}) [NC]
RewriteRule (.*) http://blog.%2/$1 [R=301,L]

28 | aequalsbb

November 7th, 2009 at 8:18 am

Avatar

@Yessica
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule (.*) http://%1yessica.example.com/$1 [R=301,L]

29 | Curtis

November 7th, 2009 at 11:56 am

Avatar

Thanks aequalsbb !

And is this htaccess file will change all the url of the http://www.domain.com ?

To be more precise, I want to know if, when other sites point to an url in a domain folder i.e :
http://www.domain.com/pictures/picture.jpg
if they still be working or do I need to move all the links coming from elsewhere to the new url:
blog.domain.com/pictures/picture.jpg
?

Will it break all the links or is it just a kind of redirect for the people coming to my site http://www.domain.com and domain.com to lead them automaticly to blog.domain.com ?

to be more precise I just want to "redirect" the homepage to the blog subdomain and NOT rewrite the url.

for the moment I use the html hack

but it's not reallt SEO friendly at all.

I want to do this in a clean way with htaccess redirection.

thanks

30 | yessica

November 8th, 2009 at 1:01 am

Avatar

Thanks , But i need this.

i don't very good with .htaccess.

my domain is:

http://www.example.com

but i want this.

http://www.yessica.example.com -> http://www.example.com/usuario.php=yessica

helpme please.

31 | vijay

November 8th, 2009 at 10:57 pm

Avatar

Hello friends, Please Help me, I posted a question, but nobody given answer for that, please help, I repeat Question that,
I have url http://subdomain.mydomain.com and want to redirect them http://mydomin.com/folder1/index.php?user=subdomain. but I dont want to change URL in addressbar so in addressbar the url should be http://subdomain.mydomain.com. What i have to do? My .htaccess code is
=================================
#RewriteCond %{HTTP_HOST} !^www\.mydomain.com [NC]
#RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain.com [NC]
#RewriteRule ^(.*)$ http://mydomain.com/folder1/index.php?user=%1 [L]
=================================
It redirect correctly, But Unfortunitly, it affect addressbar, so it's useless for me. Please help me. What I have to do?
Please Help me, I have some Urgent.
THanks.

32 | aequalsb

November 9th, 2009 at 10:54 am

Avatar

@vjay

the RewriteRule is an http redirect – which will ALWAYS affect the address bar (as it should to be anti-spoof)

there are a few options:
1. the nasty frame option
at mydomain.com, you will need a frameset file with a single frame that fills the entire frame. in that frame you will reference subdomain.mydomain.com and it will not affect the address bar. however, it's messy, easily bypassed, and terribly SEO unfriendly.

2. the PHP readfile() option
this one is complicated and requires real PHP skills (chicks dig guys with great nunchuck and PHP skills). your .htaccess file will redirect all queries to a LOCAL PHP file at mydomain..com. that PHP file takes the URL query, performs some routines to come up with an http path to subdomain.mydomain.com, and then uses readfile() to read the file from that path and output it to the http stream WITHOUT changing the URL. the only real limitation to readfile() is that the server you are reading FROM MUST HAVE the allow_url_fopen flag in php.ini to be set to 'On' — obviously this is beyond the scope of this post and you can continue down that path on your own. here is a link for reference to get you going.
http://us2.php.net/manual/en/function.readfile.php

3. the final option
if you can't do option #1 or option #2 – the final option… is just live with it… sorry…

33 | Vijaya Kumar S

December 1st, 2009 at 10:15 pm

Avatar

@vijay I had written htaccess some lines for you. I don't know, whether it's works or not because i did't tested till now. So, try this..

RewriteEngine On
RewriteCond %{HTTP_HOST} ^lalit\.example\.com
RewriteRule ^(.*)$ /index.php?screenname=$1 [L]

34 | Lexy

December 3rd, 2009 at 6:03 pm

Avatar

Hello, I was wondering if anybody could help me. I'm a dunce at this and can't get anything I try to work with right.

I want to be able to redirect http://sub.domain.com/folders/ to http://sub.domain.com/file.php. I have three specific folders I want to redirect to that one specific file; I don't want all the folders to redirect. How should I go about it? Help, anyone? Would be greatly appreciated. Thanks in advance!

35 | udham

January 3rd, 2010 at 6:40 am

Avatar

want to move from http;//artsedg.weebly.com

to my own unregistered site http://artsedg.com by

redirection, any help . THANQS in advance

36 | Jennifer

January 4th, 2010 at 8:44 am

Avatar

@udham – normally I'd suggest doing some sort of redirect – but I don't think weebly lets you have that kind of control.

37 | aequalsb

January 5th, 2010 at 9:30 am

Avatar

you will not be able to do what you want unless you have FTP access at weebly (to be able to create the .htaccess file there) AND weebly will honor localized .htaccess files.

you can, however, do a PHP redirect (unless you have NO FTP access). you would need something like this in the index.php file.

there may be additional workarounds using whatever CMS is provided by weebly (i have no idea). the thing you're looking for is he ability to upload or create a .htaccess file or an index.php file AT weebly.

38 | aequalsb

January 5th, 2010 at 9:31 am

Avatar

the PHP code was zapped in the above post.

here it is again:
<?php

header( 'http://artsedg.com' );
exit;

?>

39 | aequalsb

January 5th, 2010 at 9:33 am

Avatar

the PHP code was zapped in the above post.

put this code in your index.php file between the "PHP tags":
—–
header( 'http://artsedg.com' );
exit;
—–

40 | Andrew

January 16th, 2010 at 10:41 am

Avatar

I have an idea for anyone who is looking to have 'subdomain.domain.com' redirect to 'domain.com/subdomain' BUT still have 'subdomain.domain.com' remain in the URL. I didn't use htaccess. I used PHP instead.

1. Add a wildcard DNS setting to your domain. (*.domain.com) I did this using my plesk control panel. Simply add a new DNS record with *.domain.com and the domain's IP.

2. In the root of domain.com, put an index.php file that parses the url and extracts the subdomain. I simply used:
$url = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = $url[0];

… which isn't the best but fine for now.

Once I extracted the subdomain, I simply did:

require_once $subdomain.'/index.php';

It works well. Does anyone see any problems with this?

41 | epool86

February 2nd, 2010 at 12:27 am

Avatar

what i want is, either "blog.domain.com" or "www.blog.domain.com" to be rewrited (not redirected) to "/blog" directory.

so far, this is my htaccess

RewriteCond %{HTTP_HOST} ^(www\.)?(blog)\.domain\.com$
RewriteRule ^(.*)$ %2/ [NC,L]

and its work fine (i already set wildcard subdomain on my hosting).

but the problem is i want URL "blog.domain.com/anythingHere" to be rewrited to "/blog/anythingHere" directory

i dont know how to do it. i tried the following script but it resulting an 500 internal server error

RewriteCond %{HTTP_HOST} ^(www\.)?(blog)\.domain\.com$
RewriteRule ^(.*)$ %2/$1 [NC,L]

can someone help me please? after 2 days i still haven't found the solution yet :-(

42 | jerey

March 11th, 2010 at 2:42 am

Avatar

how do i rewrite this because it tried

RewriteEngine on
#Options +FollowSymlinks

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
RewriteBase /careers/admin
ErrorDocument 404 /page-unavailable/

i want to access

http://careers.mydomain.com/admin

43 | natacha

January 5th, 2011 at 1:16 am

Avatar

I want this please:

my domain is:

http://www.example.com

my subdomain is:

service.example.com

i want this

http://www.service.example.com -> http://www.example.com

44 | hardik

February 16th, 2011 at 11:34 am

Avatar

I need same like this

anyone help me

my domain is:

http://www.example.com

but i want this.

http://www.yessica.example.com -> http://www.example.com/usuario.php=yessica

helpme please.

45 | carrie

February 15th, 2012 at 7:42 am

Avatar

A lot of these comments seem very close to what I'm looking for. I'm not a developer, but have figure how how to create a very comprehensive .htaccess file that's redirecting a lot of old website pages. My issue now is that I have several subdomains, but need to redirect only one. I'm using this code:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

This is successful in redirecting ONLY subdomain.domain.com, but NOT subdomain.domain.com/xyz. I need to redirect everything from the subdomain.

Any suggestions?

46 | aequals

February 15th, 2012 at 11:50 am

Avatar

honestly i can't explain why the code you present does not work as you say. i do see some unneeded regular expression markup. compare yours to the one below:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

47 | Anurag

June 9th, 2012 at 9:54 am

Avatar

Suppose I have a mydomain.com and there is a folder test on the server.

When someone opens anything.mydomain.com it should stay anything.mydomain.com on browser but should call files from mydomain.com/test folder…

any idea how can i do that?

Comment Form

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements