Forum Moderators: phranque

Message Too Old, No Replies

Help requested for 301 Redirect using .htacesss

Its a complex redirect. Please help...

         

S0ha1L

7:43 pm on May 1, 2010 (gmt 0)

10+ Year Member



Hi, i need little help in setting a 301 redirect to my 404 pages.

Current URL:
http://example.com/1234/56/abc/&rurl=translate.google.com&lang=en&usg=blahblah%2F/page/4/

All data is in the above url chages randomly except http://example.com and page word. Please try to use regular expressions for others.

Notice "%2F" in the above URL

Want to redirect it to:

http://example.com/1234/56/abc/

I have tried the redirect using 301 but %2F always gives the error. Thankyou for your help.
Update:
Probably "%2F" means another /

So the URL becomes

http://example.com/1234/56/abc/&rurl=translate.google.com&lang=en&usg=blahblah//page/4/

Which should have a 301 redirect at http://example.com/1234/56/abc/

Update:
I only want pages with the following format

http://example.com/****/**/******/************%2F/page/**/

to redirect to

http://example.com/****/**/******/

[edited by: jdMorgan at 2:46 am (utc) on May 6, 2010]
[edit reason] example.com [/edit]

S0ha1L

6:44 am on May 2, 2010 (gmt 0)

10+ Year Member



Any body here?

g1smd

6:59 am on May 2, 2010 (gmt 0)

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



As per forum charter, please provide your code, along with specific questions about the problems you are having with your code.

What should it do? What did it actually do? How does that differ from what you wanted?

You'll be using RewriteRule with [R=301,L] flags and a preceding RewriteCond to test the QUERY_STRING value.

There's many hundreds of prior example threads covering similar questions to those you raise above. Let's see your code.

S0ha1L

9:25 am on May 2, 2010 (gmt 0)

10+ Year Member



Ok. Here is wordpress plugin which helped me until now. I hv set the following regular expression in it. Now how to write the following conditions in .htaccess file?

Source URL: /(\d*)/(\d*)/(.*)/(.*)/page/(\d*)/
Target URL: /$1/$2/$3/
HTTP Code: 301

Sample URL:

http://example.com/1234/56/abc/&rurl=translate.google.com&lang=en&usg=blahblah%2F/page/4/

After Redirection:

http://example.com/1234/56/abc/

[img690.imageshack.us...]

Hope it helps! Hope you will reply soon! Thanks

S0ha1L

12:50 pm on May 2, 2010 (gmt 0)

10+ Year Member



Please reply! Google is continuously indexing wrong permalinks of my site. Help!

S0ha1L

12:53 pm on May 2, 2010 (gmt 0)

10+ Year Member



Can anyone tell me what code should i write in my .htaccess file which will redirect

http://example.com/1234/56/abc/&rurl=translate.google.com&lang=en&usg=blahblah%2F/page/4/

to

http://example.com/1234/56/abc/

jdMorgan

3:17 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This forum has a very limited number of contributors. Therefore, we can help you with *your* redirect code, but we cannot write it for you.

As outlined in our Forum Charter [webmasterworld.com], our purpose here is to discuss and educate, and not to try to serve as some kind of "global help desk" or "free code-writing service." As a professional Webmaster, I believe you can appreciate the folly of trying to do that in a forum with fewer than six regular contributors.

This is not at all a complex problem, and the only difficulty is that the URL-path contains an ampersand *which is not* part of a query string. Either this ampersand can be matched directly as an un-encoded "&" character using a regular expression in a RewriteRule pattern, or perhaps a RewriteCond examining the variable %{THE_REQUEST} will be needed to test for percent-encoded characters such as %26 and %2f, or their multiply-encoded forms such as %2526 and %2525252f for example.

Having detected the incorrect character(s) you can 301-redirect the request to the correct URL, rewrite it to a non-existent internal filepath in order to force a 404-Not Found response, or directly return a 410-Gone response.

Either way it's one or two lines of code in mod_rewrite, which hardly qualifies as a difficult problem.

Please post your best effort at coding and testing your code as a basis for on-going discussion. You will then get much more useful responses. Our Forum Charter has links to several very-useful resources.

Thanks,
Jim

S0ha1L

3:33 pm on May 2, 2010 (gmt 0)

10+ Year Member



@jdMorgan i tried my best Sir and told to you in the above posts, i have almost no idea about .htacess editing. My website is getting falsely indexed by Google day by day due to such errors. I hope you will understand my condition. I appreciate your reply!

S0ha1L

2:23 pm on May 4, 2010 (gmt 0)

10+ Year Member



@jdMorgan, ok lets make it simple

How can i redirect

http://example.com/1234/56/abc/anything

anything = words, symbols, numbers etc

to

http://example.com/1234/56/abc/

?

S0ha1L

2:25 pm on May 4, 2010 (gmt 0)

10+ Year Member



I mean redirect to

http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/

whenever we find

http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/anything1/anything2/....

S0ha1L

4:51 pm on May 5, 2010 (gmt 0)

10+ Year Member



Thankyou guys for your help. I sorted out the problem.

g1smd

5:09 pm on May 5, 2010 (gmt 0)

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



Now you have some code to discuss, let's see it and optimise it.

S0ha1L

5:33 pm on May 5, 2010 (gmt 0)

10+ Year Member



I used PHP instead of .htaccess for 301 redirect in my wp-blog

This is an example URL:
http://example.com/1234/56/abc/&rurl=translate.google.com&lang=en&usg=blahblah

if ( stripos($_SERVER['REQUEST_URI'],'rurl=') !== false) {
$urlpart = explode('rurl=',$_SERVER["REQUEST_URI"],2);
wp_redirect(rtrim($urlpart[0],"?&"),301);
exit;
}

I have applied many filters and all are working fine.

But whenever there is %2F in URL, it still gives 404. Same problem is discussed here [webmasterworld.com...]

jdMorgan

3:17 am on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to url-decode it before you explode it. Not sure of the syntax myself, though.

Jim

S0ha1L

8:38 pm on May 8, 2010 (gmt 0)

10+ Year Member



Ok, i think now i need help. Its not working..

Redirect
http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/anything1/anything2/....

to

http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/

RedirectMatch 302 /([0-9]+)/([0-9]+)/([A-Z,a-z,0-9]+)/(.*)$ http://example.com/$1/$2/$3/

S0ha1L

8:48 pm on May 8, 2010 (gmt 0)

10+ Year Member



RedirectMatch 302 /([0-9]+)/([0-9]+)/(.*)/(.*)$ http://example.com/$1/$2/$3/

This results in an infinite loop for http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/

g1smd

9:42 pm on May 8, 2010 (gmt 0)

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



Of course it does, as the new URL matches the pattern again and is redirected again. Use HTTP Live Headers for Firefox to see exactly what happens.

Your use of (.*) patterns matches "something, anything, or nothing", and so matches any request with or without a trailing slash.

Use more specific patterns, especially those like ([^/]+)$ or similar.

Why do you want a 302 redirect here? What's your reasoning?

S0ha1L

10:14 pm on May 8, 2010 (gmt 0)

10+ Year Member



Thats actually 301, i mistakenly wrote 302. Yeah g1smd

([^/]+) whats the + sign mean here and how can i represent an alpha-numeric string with regular expressions?

S0ha1L

10:17 pm on May 8, 2010 (gmt 0)

10+ Year Member



Can anybody simply explain how to correctly redirect

http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/anything1/anything2/....

to

http://example.com/[4-digit-number]/[2-digit-number]/[alpha-numeric-string]/

I guess i asked the same question for the 6th time :S

g1smd

10:24 pm on May 8, 2010 (gmt 0)

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



As I said above, don't match "anything", try something else.

The + means "1 or more times", so
/([^/]+)$
means, "slash, followed by one or more characters that are not a slash, then end". This will match something that does not end in a slash.

You could also try
/([^/.]+)$
which ensures it also doesn't match a filename type URL with extension either.

Don't mix RewriteRule and RedirectMatch directives here. Use RewriteRule for all of your rules.

There's 68 000 threads in this forum, and all of these things have been covered hundreds of times before.