TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   redirecting to subdomain via htaccess (http://www.talkphp.com/general/2412-redirecting-subdomain-via-htaccess.html)

Dan 03-04-2008 01:07 AM

redirecting to subdomain via htaccess
 
Hello everyone! :-)

Question. I have over 2000 URLs I'm trying to successfully 301 redirect to subdomains using the root htaccess file.

Code:

domain.com/same/NAME to NAME.domain.com
Note the "same" directory is a constant throughout all the URLs.

Instead of writing each URL line by line:
Redirect 301 /same/1 http://1.domain.com
Redirect 301 /same/2 http://2.domain.com
Redirect 301 /same/3 http://3.domain.com

I was thinking is it possible to redirect it all using 1 line, like:
Code:

Redirect 301 /same/$1 http://(.*).domain.com
EDIT: I already have the rewrite of the subdomains working, so just to clarify. All i'm looking to do is redirect the old pages to the new ones.

Salathe 03-04-2008 01:17 AM

It might not be as simple as copy/paste but a starting point might be along the lines of:

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

Dan 03-04-2008 01:22 AM

Worked a treat. Cheers Salathe.

Dan 03-04-2008 02:54 AM

Code:

RewriteRule ^sample/(.*)/ http://$1.domain.com [R=301,L]
RewriteRule ^sample/(.*) http://$1.domain.com [R=301,L]

I've had to use two lines to get it redirecting with a trialing slash and not.

So domain.com/sample/page and domain.com/sample/page/ both redirect to the same page.

Is there anyway to merge those two lines?

Salathe 03-04-2008 01:30 PM

Since we're afforded the yumminess that is regular expressions, we can simply specify that the trailing slash is optional with the question mark character (?). Also, will the varying values only ever be numbers? Or, would you like to only capture what's between sample/ and the next forward slash (if there is one)? I'm just asking because it's better to make the matching expression as specific as possible! At the moment sample/my/sub/domain.html would try to redirect to http://my/sub/domain.html.domain.com which I'm fairly sure you don't want.

Anyhow, to answer your question:
RewriteRule ^sample/(.*)/? http://$1.domain.com [R=301,L]

Dan 03-04-2008 02:14 PM

There is a mix of letters numbers and -.

When going to domain.com/sample/page/ it redirects to: page.com/.domain.com umm


All times are GMT. The time now is 06:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0