Forum Moderators: phranque

Message Too Old, No Replies

301 redirect not working pls help

         

ArchonElite

11:16 am on Apr 30, 2012 (gmt 0)

10+ Year Member



Hello. I have a 301 redirect problem. I am familiar with the 301 redirect and this is my code for a testing server.

old page: http://example.com/blognews/?category=news
new page: http://example.com/news/

RewriteCond %{QUERY_STRING} ^category=news$
RewriteRule ^blognews/$ http://example.com/news/? [R=301,L]

Can someone tell me where and what i have done wrong?

lucy24

4:42 pm on Apr 30, 2012 (gmt 0)

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



:: peering into crystal ball ::

No, because you haven't told us what happens. The crystal ball does offer one possibility: if mod_index has already executed, then you will never see /blognews/$ with closing anchor. If blognews/ is a real directory, it will come through as /blognews/index.php or similar. You can easily test this by leaving off the closing anchor in the pattern.

ArchonElite

4:48 pm on Apr 30, 2012 (gmt 0)

10+ Year Member



Well thing is i dont have access to the old site and i dont know if it is a directory or not. How can i make my redirect work? :(

g1smd

7:44 pm on Apr 30, 2012 (gmt 0)

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



The redirect should be placed at a place that the old domain name resolves to. It cannot possibly work otherwise.

lucy24

1:29 am on May 1, 2012 (gmt 0)

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



Uhm, what do you mean by old site? Different domain name, or different physical location (like changing servers)? Your first post gives the same domain name for both old and new.

If you're talking about two different domains but still need to prevent auto-linking, you can call them

http://www.example.com
http://www.example.org
http://www.example.net
http://www.example.co.uk
http://www.example.ca
http://www.example.mx

... and so on :)

If you need to redirect from one domain to a different domain, you MUST have access to the old domain. Or at least to where the DNS thinks the old domain is. It doesn't have to contain all the old files, so long as the DNS points to something, because that's where the Redirect has to happen.

ArchonElite

8:40 am on May 1, 2012 (gmt 0)

10+ Year Member



There are two different hosts but it is the same domain. The old host i dont have access anymore only to the new host. So far i know how to redirect a normal dynamic url to a static one i did it like this and it worked.

old page: http://example.com/index.php?func=PageForgottenPassword
new page: http://example.com/news/

RewriteCond %{QUERY_STRING} ^func=PageForgottenPassword$
RewriteRule ^$ http://example.com/news/? [R=301,L]

As i sayd i dont know how to do it if i have the example posted before, when the dynamic link comes after blognews/?category=news . I tryed in many ways and still nothing. I will say this again it is the same domain but different hosts(servers or ip's). Can someone tell me how can i fix this issue? On other websites other people had the same issue and they were told to come here and post the problem cause most likely here it will get an answer.

lucy24

6:52 pm on May 1, 2012 (gmt 0)

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



For starters: It doesn't matter that your domain has moved to a new physical address. As far as the Internet is concerned, it's just like calling someone on their cell phone: you'll reach them, no matter where they are.

If requests for your domain aren't arriving at the new location, that's a DNS problem. You can't solve it by redirecting. If requests are arriving, then the changed-host issue is a red herring and you don't even need to think about it.

It sounds as if what you are looking for is the redirect-to-rewrite two-step, where the user sees a short pretty URL while you serve content from a long complicated URL.

:: shuffling papers ::

Here is the boilerplate. It is up to you to adjust the details to fit your specific site.

The Redirect-to-Rewrite Two-Step

Problem: Your dynamically generated pages have long, ugly, hard-to-memorize URLs, probably containing query strings. You want them to have short pretty URLs.

The Solution comes in two parts.

Part 1. Redirect
When a user asks for the long ugly URL, redirect to the short pretty URL. Basic pattern:

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} queryname=([a-z]+)
RewriteRule longcomplicatedURL http://www.example.com/blahblah/%1? [R=301,L]


The %1 is captured from the original query string, and the final ? means that you now get rid of the query string. In real life it will usually be a little more complicated, but that's the basic process.

Example:
user asks for
www.example.com/directory/morestuff/index.php?model=volvo


They get bounced over to
www.example.com/cars/volvo


Part 2. Rewrite
You get an incoming request for a short pretty URL-- either from a new arrival or from someone who was redirected in Part 1. The server can't tell the difference.

RewriteRule blahblah/([a-z]+)$ longcomplicatedURL?queryname=$1 [L]


This time around, you're capturing part of the request and changing it into a query string.

Example:
user asks for
www.example.com/cars/volvo


They may think that's what they're getting-- it's what the browser's address bar says-- but behind the scenes the page content is really coming from
www.example.com/directory/morestuff/index.php?model=$1


Now you see why Part 1 had to look at THE_REQUEST. It's for insurance. If something happens later on, your long complicated URL might pass through mod_rewrite again. If it does, you need to be sure it doesn't get re-redirected. Otherwise there will be an infinite loop.

Now wait a minute! Does this mean that if someone starts out asking for "longcomplicatedURL", they go through this whole rigamarole and then they end up right back where they started?

Yup. But they don't know it. They only know what the browser's address bar tells them. Even robots-- yes, even google-- can't tell that they're being rewritten.

The Redirect part of the package-- Part 1-- is not technically necessary. The Rewrite-- Part 2-- will function without it. But redirecting everyone to the same URL means that everyone is now on the same page ... and it avoids nasty things like Duplicate Content.

But you're not done yet.

Part 0.
Before you do anything with Part 1 and Part 2, go over your current site carefully. Make sure that your own links point only to the short pretty URL. Requests for the long complicated URL should come only from outside-- from people with outdated bookmarks, or old links from other sites. Your own site will use only the pretty URLs.

g1smd

10:24 pm on May 1, 2012 (gmt 0)

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



old page: http://example.com/index.php?func=PageForgottenPassword
new page: http://example.com/news/

RewriteCond %{QUERY_STRING} ^func=PageForgottenPassword$
RewriteRule ^$ http://example.com/news/? [R=301,L]

There's no way the above code can do what you say it does. The pattern
^$
never matches a request containing
index.php
.

The code would work for a URL request like this:
http://example.com/?func=PageForgottenPassword
where
index.php
is not stated.

d3vin

5:09 am on May 7, 2012 (gmt 0)

10+ Year Member



redirect 301 /blognews/?category=news http://example.com/news/

i think that'll do...

g1smd

6:39 am on May 7, 2012 (gmt 0)

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



You think?

Did you test it?

ArchonElite

8:26 am on May 7, 2012 (gmt 0)

10+ Year Member



Still don't know how to do it. I mean it doest work. What lucy24 sayd i don't know how to write the code. I tried several times and failed, nothing happen the way i expected.

As i sayd i want something like this:

Old link: http://example.com/blognews/?category=view

The link that i want: http://example.com/news

The link that i get with so many redirect codes i tried is this: http://example.com/news/?category=view

Can someone write a 301 redirect here with the proper code that will work? I will appreciate the help guys :(

lucy24

3:28 pm on May 7, 2012 (gmt 0)

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



Can someone write a 301 redirect here

Nope, we'll force you to keep trying until you get it right.

She said, with a straight face, after bringing her son's two sites crashing to the ground with a misplaced </Files>.

Like standing over your kid for three hours while they clean their room :) so they can grow up to be parents who stand over their kids et cetera.

Re-read the boilerplate, paying close attention to what it says about the query string. That's your key.

g1smd

5:08 pm on May 7, 2012 (gmt 0)

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



Look at the resulting URL using your code.

It looks like you need to "clear the query string".

You're one character out.