Forum Moderators: phranque

Message Too Old, No Replies

Redirect - works for one, not the other.

         

superjacent

5:02 am on May 14, 2008 (gmt 0)

10+ Year Member



Hope someone can help, as I'm going around in circles.

I'm wanting to redirect certain pages from my old site to specific pages of my new site. The .htaccess file at my old site includes the following snippet which effectively redirects back to the root of the old site.

RewriteCond %{HTTP_HOST} ^oldsite.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.net$
RewriteRule ^cms$ http://oldsite.net [R=301,L]

The above works, I merely mention this as this is what precedes what I want to do.

I've got the following redirection working

redirect 301 /node/509 http://newsite.org/node/23?

The above doesn't trap for htpp://oldsite.net/?=node/509, so I thought this would work, it doesn't.

redirect 301 /\?q=node/509 http://newsite.org/node/23?

I get 500 server errors when this is included.

How do I correctly write for the "?q=node/509" part. Also, when not including the question mark (?) at the end of the redirect line the redirect attempts to append the query part of the url. Including the ? the ? is visible in the newsite.net url, how do I removed the ? from the newsite page.

Any advice appreciated.

[edited by: jatar_k at 11:26 am (utc) on May 14, 2008]
[edit reason] delinked [/edit]

jdMorgan

2:51 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The Redirect directive of mod_alias does not handle query strings. Use mod_rewrite instead.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=node/509$
RewriteRule ^$ http://newsite.org/node/23? [R=301,L]

This assumes you're putting the code into oldsite.net/.htaccess

To clarify, query strings are not part of a URL; They are data attached to a URL to be passed to the resource at that URL. Therefore, support is not always present, or implemented the way you might expect it to be.

Jim

[edited by: jdMorgan at 2:58 pm (utc) on May 15, 2008]

superjacent

1:06 am on May 16, 2008 (gmt 0)

10+ Year Member



Thanks, works like a charm.

jdMorgan

2:49 am on May 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that you can speed up that first rule by making "www\." optional in the first RewriteCond, and thus eliminating the need for a second RewriteCond:

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.net
RewriteRule ^cms$ http://oldsite.net/ [R=301,L]

Note that I removed the end-anchor from that pattern as well. It is possible, if not likely, that you might see a request for "http://www.oldsite.net.:80/cms"

This hostname includes an optional dot after the tld, and an optional port number. Either or both are valid.

If you feel you must use an end-anchor then use this pattern:
(www\.)?oldsite.net.?(:[0-9]{1,5})?$

That allows for the optional dot and any valid port number. (You could also use (www\.)?oldsite.net.?(:80¦:433)?$
for just the common HTTP and HTTPS ports.)

Change the broken pipe character "¦" to a solid pipe before use; Posting on this forum modifies the pipe characters.

I also added an end-slash to the redirect URL to make it "correct" and avoid a server-internal redirect.

You should follow this rule with a general one to redirect *all* www.oldsite.net requests to oldsite.net, in order to avoid duplicate-content issues in search. Looking at your existing rule, I assume that you prefer the "non-www" domain, and that your non-www URLs have the most/best incoming links and rank best in search.

If you have no other subdomains except "www", then


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

should do.

Jim

superjacent

7:09 am on May 16, 2008 (gmt 0)

10+ Year Member



Thanks, very helpful.