TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-04-2008, 01:07 AM   #1 (permalink)
Dan
The Wanderer
 
Dan's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 15
Thanks: 6
Dan is on a distinguished road
Default 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.
Dan is offline  
Reply With Quote
Old 03-04-2008, 01:17 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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]
Salathe is offline  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
Dan (03-04-2008), SOCK (03-04-2008)
Old 03-04-2008, 01:22 AM   #3 (permalink)
Dan
The Wanderer
 
Dan's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 15
Thanks: 6
Dan is on a distinguished road
Default

Worked a treat. Cheers Salathe.
Dan is offline  
Reply With Quote
Old 03-04-2008, 02:54 AM   #4 (permalink)
Dan
The Wanderer
 
Dan's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 15
Thanks: 6
Dan is on a distinguished road
Default

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?
Dan is offline  
Reply With Quote
Old 03-04-2008, 01:30 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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]
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dan (03-04-2008)
Old 03-04-2008, 02:14 PM   #6 (permalink)
Dan
The Wanderer
 
Dan's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 15
Thanks: 6
Dan is on a distinguished road
Default

There is a mix of letters numbers and -.

When going to domain.com/sample/page/ it redirects to: page.com/.domain.com umm
Dan is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:43 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design