Forum Moderators: phranque

Message Too Old, No Replies

htaccess strategy for subdomains

need advice on redirection and rewrites

         

AlanW

3:14 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



I have a situation where I'm migrating a site from one CMS (Wordpress) to another. I'm trying to preserve the links for SEO and would like to 301 the links to a new url structure.

An example of the previous url structure is:

http://subdomain.domain/2011/02/25/article-title


The new url structure needs to be:

http://www.domain/subdomain/article-title


Essentially need to re-route the subdomain and remove the timestamps. First thoughts are to point the existing subdomains to the new host and reroute them there via htaccess.

Advice appreciated.

Thanks, Alan

g1smd

3:20 pm on Feb 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, it's just a couple of lines of mod_Rewrite code - assuming all the URLs follow the same sort of pattern.

The redirect from old URL to new URL must happen in a single step. You must avoid a multi-step redirection chain.

What code have you tried so far? There's more than 20 000 examples of redirect code and discussion in previous forum postings.

It is important that you fully understand all of the code you put on your server, as you will have to maintain it.

AlanW

3:28 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



For the top level I'm using:

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


but I'm not sure how to eliminate the timestamps, which vary based on date.

AlanW

5:19 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



Could I try something like this? Please feel free to point out my ignorance. That's why I'm here.

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

g1smd

6:37 pm on Feb 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %\{HTTP_HOST\} ^subdomain.domain.com$ [OR]
RewriteCond %\{HTTP_HOST\} ^www.subdomain.domain.com$


simplifies to

RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com


Don't forget to escape literal periods in patterns as \. here. Remove the $ symbol from the end of the pattern otherwise requests with a port number will not be redirected.



Assuming you want to remove the date as you redirect, the

^(.*)$


pattern should change to:

^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$

AlanW

7:05 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



Thanks g1smd. So the code would be:

RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com 
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ http://www.domain.com/subdomain/%{REQUEST_URI}? [R=301,L]


If mod_rewrite doesn't find anything that applies to the RewriteRule what does it do? Meaning if someone goes from subdomain.domain.com - without a timestamp - will they still end up at domain.com/subdomain/?

g1smd

7:47 pm on Feb 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Replace

%{REQUEST_URI}


with

$1


here. I typed that out earlier in another tab, but I forgot to paste it here.

AlanW

8:21 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



OK. Will this also handle the top level redirection, so that
http://subdomain.domain.com/
301s to
http://domain.com/subdomain/
?

g1smd

11:27 pm on Feb 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are www and non-www acually two separate sites?

The code I supplied, redirects both non-www and www on the old domain to www on the new domain.

If www and non-www are the same site, you'll also need a simple non-www to www redirect on the new site.

g1smd

11:31 pm on Feb 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you need to separately map non-www => non-www and www => www use this:

RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ http://%1domain.com/subdomain/$1? [R=301,L]

AlanW

2:43 pm on Feb 28, 2011 (gmt 0)

10+ Year Member



I've done some testing with this code but am running into an issue.

The subdomain on the old server wants to find a subdomain on the new server, so it 404s. I can't create a new subdomain in which to put the redirection because our CMS won't allow it.

Do I understand that the code sample should be at the root of the inbound server? And the A records for the subdomain's DNS should reflect the root of the new server?

g1smd

8:13 pm on Feb 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Modify the redirect target in the code to point to the place you want it to point.

Instead of %1, hard code whatever it is meant to be.

AlanW

4:14 pm on Mar 1, 2011 (gmt 0)

10+ Year Member



OK I think maybe I'm going at this wrong. Sorry I haven't been explicit about what I'm trying to accomplish, but I understand why it's important to be explicit.

What needs to happen is that the subdomain needs to be appended to the domain as a directory, and anything after the domain needs to be stripped, and the last item in the string needs to be appended after the directory.

So that:

http://subdomain.domain/2011/02/25/anything[OR]
http://subdomain.domain/anything


becomes

http://domain.com/subdomain/anything 


Or:

http://subdomain.domain/


becomes

http://domain.com/subdomain/


Basically the redirect needs to remember the last piece of the string and append it to the domain, whether something exists there or not.

g1smd

4:44 pm on Mar 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, it's a combination of the two examples previously posted, so the code will be a mixture of both of those posts.

AlanW

5:04 pm on Mar 1, 2011 (gmt 0)

10+ Year Member



Is this correct? Here I'm assuming that .* represents the end of the string, and $1? represents the variable defined by .* ?

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

jdMorgan

9:04 pm on Mar 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

# Externally redirect requests for subdomain.domain.com/<any_number_of_directory_levels>/<last-path-part>
# to domain.com/subdomain/<last-path-part>, also removing any appended query strings
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com
RewriteRule ^([^/]+/)*(.*)$ http://www.domain.com/subdomain/$2? [R=301,L]

Note that as coded, if the "last-path-part" ends with a slash, then it will be removed and the redirect will be to www.domain.com/subdomain/. This can be changed if necessary. All depends on precisely how you wish to define "anything" and "the last item in the string" in your post above.

Jim

AlanW

9:56 pm on Mar 8, 2011 (gmt 0)

10+ Year Member



Thanks Jim,

Well yes, that is also a possibility, where the last portion of the string may have a trailing slash.

Does the * in between the groups signify that the ([^/]+/) pattern can repeat as many times as possible? How is (.*) excluded from this? Doesn't it also include slashes?

But as I said, the last part of a string may or may not have a trailing slash. So how do you signify the end of the string?

jdMorgan

7:28 pm on Mar 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the question is actually "how do *you* signify the end of the string?" -- You personally. You have two choices: URL subdomain.com/x/y refers to filepath /subdomain/y and URL subdomain.com/x/y/ refers to filepath /subdomain/, or both URL subdomain.com/x/y and URL subdomain.com/x/y/ refer to filepath /subdomain/y/

It all boils down to your "conventions" -- does a trailing slash make the "final path-part" empty, or does it just make that "final part" end with a slash? How do *you* wish to define "the trailing path-part?"

I can't tell you which to choose, as you may have pre-existing conventions to consider. However, the "correct" HTTP convention is to treat the URL-path /x/y/ as indicating the "index page" of directory /x/y/ and to treat the URL-path /x/y as indicating the page named "y" in the directory "/x"

However, it has to be one way or the other.

Jim