...

View Full Version : 301 redirect problem



nickyfraggle
03-31-2011, 06:11 PM
Hi - I'm trying to redirect old URLs with variables to new URLs with variables.

e.g.

http://www.mysite.com/blog.php?c=My Blog Is Cool

should redirect to

http://www.mysite.com/letters.php?blogname=Cool Blog

The variable on the end is important as I need it to decide which URL should be gone to next. I've only got about 10 URLs to redirect, so I don't need a dynamic script - I'm happy to do it one by one.

So far I've tried:


Redirect 301 /blog/myblog.php?c=My Blog Is Cool http://www.mysite.com/letters.php?blogname=Cool Blog

Which doesn't work as it doesn't take into account the URL variables, it just ignores them completely.

So then I tried adding this:


RewriteCond %{QUERY_STRING} ^c=My Blog Is Cool$
Redirect 301 /blog/myblog.php http://www.mysite.com/letters.php?blogname=Cool Blog


Which got me one step closer, now I'm redirecting but it's appending the old variables to the end of my URL, and does the redirect regardless of what the old URL was!

Would really appreciate some help as I'm pulling my hair out!

Thanks,
Nicky

oesxyl
03-31-2011, 08:29 PM
Hi - I'm trying to redirect old URLs with variables to new URLs with variables.

e.g.

http://www.mysite.com/blog.php?c=My Blog Is Cool

should redirect to

http://www.mysite.com/letters.php?blogname=Cool Blog

The variable on the end is important as I need it to decide which URL should be gone to next. I've only got about 10 URLs to redirect, so I don't need a dynamic script - I'm happy to do it one by one.

So far I've tried:


Redirect 301 /blog/myblog.php?c=My Blog Is Cool http://www.mysite.com/letters.php?blogname=Cool Blog

Which doesn't work as it doesn't take into account the URL variables, it just ignores them completely.

So then I tried adding this:


RewriteCond %{QUERY_STRING} ^c=My Blog Is Cool$
Redirect 301 /blog/myblog.php http://www.mysite.com/letters.php?blogname=Cool Blog


Which got me one step closer, now I'm redirecting but it's appending the old variables to the end of my URL, and does the redirect regardless of what the old URL was!

Would really appreciate some help as I'm pulling my hair out!

Thanks,
Nicky
the problem is urlencoding, you can't use spaces or other things not allowed in a url.
must be something like My%20Blog%20is%20Cool
will work with both regex and RedirectPermanent but need to be like this:


Redirect 301 /blog/myblog.php?c=My%20Blog%20Is%20Cool http://www.mysite.com/letters.php?blogname=Cool%20Blog

best regards

nickyfraggle
04-01-2011, 09:50 AM
Hi - Thanks for your help.

I have tried it without spaces (using hyphens or %20 inbetween) but it still doesn't work.
:confused:

Nicky

oesxyl
04-02-2011, 05:17 AM
Hi - Thanks for your help.

I have tried it without spaces (using hyphens or %20 inbetween) but it still doesn't work.
:confused:

Nicky
redirection work without normal links? where you use this in a .htaccess file? the server see the .htaccess?
for last two questions, is simple to check, put some garbage in .htaccess and if you get a 500 error it work, :)
for first, try a normal redirection from one page to another, to fake page, contact to info.

best regards

nickyfraggle
04-07-2011, 10:10 AM
Hi - Yes the server is definitely seeing the htaccess file. Other parts of it are working no problems.

I think the main problem I'm having now is getting it to recognise the variables on the end of the first URL. I can redirect to a new URL with variables (like you said with no spaces), but if I put variables in the first URL it doesn't work.

Very frustrating!

My code now looks like:

RewriteCond %{QUERY_STRING} ^c=My%20Blog$

Redirect 301 /blog/blogger.php http://www.mysite.co.uk/blog/myblog.php?c=My%20Blog%20is%20Cool

This works and redirects correctly, but I really need it to work with variables in the first URL too:

RewriteCond %{QUERY_STRING} ^c=My%20Blog$

Redirect 301 /blog/blogger.php?b=12 http://www.mysite.co.uk/blog/myblog.php?c=My%20Blog%20is%20Cool

This doesn't work!

Thanks again for all your help. I've been all over the internet looking.

It's appreciated.

Nicola

oesxyl
04-07-2011, 10:29 AM
Hi - Yes the server is definitely seeing the htaccess file. Other parts of it are working no problems.

I think the main problem I'm having now is getting it to recognise the variables on the end of the first URL. I can redirect to a new URL with variables (like you said with no spaces), but if I put variables in the first URL it doesn't work.

Very frustrating!

My code now looks like:

RewriteCond %{QUERY_STRING} ^c=My%20Blog$

Redirect 301 /blog/blogger.php http://www.mysite.co.uk/blog/myblog.php?c=My%20Blog%20is%20Cool

This works and redirects correctly, but I really need it to work with variables in the first URL too:

RewriteCond %{QUERY_STRING} ^c=My%20Blog$

Redirect 301 /blog/blogger.php?b=12 http://www.mysite.co.uk/blog/myblog.php?c=My%20Blog%20is%20Cool

This doesn't work!

Thanks again for all your help. I've been all over the internet looking.

It's appreciated.

Nicola

i don't think you can mix RewriteCond and Redirect to pass arguments.
Look to this two ways:

http://httpd.apache.org/docs/2.2/rewrite/advanced.html#setenvvars

http://httpd.apache.org/docs/2.2/rewrite/advanced.html

best regards

nickyfraggle
04-07-2011, 10:35 AM
I see what you mean, so I removed this line:


RewriteCond %{QUERY_STRING} ^c=My%20Blog$

It still works in the same way so it wasn't doing anything like you suggested.

How would I get the redirect to notice the variables on the end of the first URL (the one to redirect from)? I don't need to do anything with the variables or pass them on, I just need it to recognise the whole URL and redirect.

Thanks,

Nicola

p.s I'm reading through your links...tough going!!

oesxyl
04-07-2011, 11:24 AM
I see what you mean, so I removed this line:


RewriteCond %{QUERY_STRING} ^c=My%20Blog$

It still works in the same way so it wasn't doing anything like you suggested.

How would I get the redirect to notice the variables on the end of the first URL (the one to redirect from)? I don't need to do anything with the variables or pass them on, I just need it to recognise the whole URL and redirect.

Thanks,

Nicola

p.s I'm reading through your links...tough going!!

try this:



RewriteCond %{QUERY_STRING} ^c=(.*)$
RewriteRule ^blog\.php$ letters.php?blogname=%1 [R=301,L]


best regards

nickyfraggle
04-07-2011, 12:09 PM
Wow that's really close! It works, but my original URL is in a sub directory not in the root and I can't get it to look for blog.php anywhere else other than the root?

I tried:

RewriteCond %{QUERY_STRING} ^c=(.*)$
RewriteRule ^/blog/blog\.php$ letters.php?blogname=%1 [R=301,L]

but no luck. I really do hate htaccess!

Nicola

oesxyl
04-07-2011, 12:13 PM
Wow that's really close! It works, but my original URL is in a sub directory not in the root and I can't get it to look for blog.php anywhere else other than the root?

I tried:

RewriteCond %{QUERY_STRING} ^c=(.*)$
RewriteRule ^/blog/blog\.php$ letters.php?blogname=%1 [R=301,L]

but no luck. I really do hate htaccess!

Nicola
this one?


RewriteRule ^blog\/blog\.php$ letters.php?blogname=%1 [R=301,L]

best regards

nickyfraggle
04-07-2011, 12:16 PM
It works!! You are an angel!!

I do have one really small problem now - spaces don't work out right. When redirected the become:

My%2520Blog

In the URL they are entered:

My%20Blog

You have been very helpful and I really appreciate your help.

Nicola

oesxyl
04-07-2011, 12:26 PM
It works!! You are an angel!!

I do have one really small problem now - spaces don't work out right. When redirected the become:

My%2520Blog

In the URL they are entered:

My%20Blog

You have been very helpful and I really appreciate your help.

Nicola
%2520 means after decoding %20, you have % somewhere?

yes you have, :)
the url in the address bar: "My%20Blog", use "My Blog" instead


best regards

nickyfraggle
04-07-2011, 12:29 PM
It even does it if I remove the %20 from the URL and just leave the space e.g.My Blog still goes to My%2520Blog.

I'm happy to replace the space with a hyphen if that helps?

Nicola

oesxyl
04-07-2011, 12:33 PM
It even does it if I remove the %20 from the URL and just leave the space e.g.My Blog still goes to My%2520Blog.

I'm happy to replace the space with a hyphen if that helps?

Nicola
clear the browser cache and try again, i can bet is a browser problem, :)
but yes, a '-' would be better

best regards

nickyfraggle
04-07-2011, 12:35 PM
Ok I'll clear my cache. I'm looking up how to add a hyphen to replace the space in the new URL.

This .htaccess stuff is a real headache! Really appreciate your time today.

Nicola

oesxyl
04-07-2011, 12:36 PM
Ok I'll clear my cache. I'm looking up how to add a hyphen to replace the space in the new URL.

This .htaccess stuff is a real headache! Really appreciate your time today.

Nicola
you are welcome, :)

best regards

nickyfraggle
05-03-2011, 04:38 PM
Hi - Still struggling with this! My code now reads:


RewriteCond %{QUERY_STRING} ^b1=(.*)$
RewriteRule ^blog\/blogger\.php$ /blogger/new-blog.php?b=%1 [R=301,L]

The redirect works, but the problem is the URL it redirects to if there is a space in the first URL e.g.

http://www.myblog.co.uk/blog/blogger.php?b1=My Blog Name

Gets forwarded to:

http://www.myblog.co.uk/blogger/new-blog.php?b=My%2520Blog%2520Name

Which doesnt make any sense and errors.

I'd like it to replace all spaces with a - so it should go to:
http://www.myblog.co.uk/blogger/new-blog.php?b=My-Blog-Name

The problem is that it seems to automatically add the %20 to replace the space which messes up the htaccess bit! I've tried everything I've found online and haven't had any luck at all!

Any help appreciated!

Nicola

oesxyl
05-03-2011, 05:45 PM
Hi - Still struggling with this! My code now reads:


RewriteCond %{QUERY_STRING} ^b1=(.*)$
RewriteRule ^blog\/blogger\.php$ /blogger/new-blog.php?b=%1 [R=301,L]

The redirect works, but the problem is the URL it redirects to if there is a space in the first URL e.g.

http://www.myblog.co.uk/blog/blogger.php?b1=My Blog Name

Gets forwarded to:

http://www.myblog.co.uk/blogger/new-blog.php?b=My%2520Blog%2520Name

Which doesnt make any sense and errors.

I'd like it to replace all spaces with a - so it should go to:
http://www.myblog.co.uk/blogger/new-blog.php?b=My-Blog-Name

The problem is that it seems to automatically add the %20 to replace the space which messes up the htaccess bit! I've tried everything I've found online and haven't had any luck at all!

Any help appreciated!

Nicola
http://www.myblog.co.uk/blogger/new-blog.php?b=My%2520Blog%2520Name is the result of double escaping, first is scaped to http://www.myblog.co.uk/blogger/new-blog.php?b=My%20Blog%20Name then % is scaped to %25.

i don't know if is possible to translate chars using mod rewrite, the regex used is only a subset and matching a variable number of spaces is difficult and you don't have another way to translate chars.

you could try to build a kind of controller, you redirect all url to a php file and there using $_SERVER['REQUEST_URI'] to change the url and redirect again but you need to do this in a way so that the new redirection to not be processed again by the php file. To be honest i don't like at all this idea, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum