Forum Moderators: phranque

Message Too Old, No Replies

Help please with htaccess rewrite rule

         

bramley

9:32 pm on Apr 5, 2011 (gmt 0)

10+ Year Member



I am getting many erroneous urls from other websites where the link is badly written and of the form :

correct url[space]title

so I want to detect the space and cut off the space and text to leave just the actual url.

I thought this would be a common need but I couldn't find a suitable rewrite rule.

Help please ...

g1smd

9:37 pm on Apr 5, 2011 (gmt 0)

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



What have you tried so far?

This should be a fairly simple one or two line snippet of code.

bramley

9:49 pm on Apr 5, 2011 (gmt 0)

10+ Year Member



Not much ;)

RewriteRule ^.html\ ([^/]+/)+

as the match but no idea what to do to cut the url / say to use just the first part

g1smd

9:53 pm on Apr 5, 2011 (gmt 0)

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



That rule will match only a URL request such as:

www.example.com/.html<space><characters>/ ..etc..

[edited by: g1smd at 9:58 pm (utc) on Apr 5, 2011]

bramley

9:53 pm on Apr 5, 2011 (gmt 0)

10+ Year Member



Hmm, maybe :

RewriteRule ^\.html$\ / [R=301,L]

g1smd

9:58 pm on Apr 5, 2011 (gmt 0)

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




Something like this might be useful:

RewriteRule ^([^\ ]+)[\ ]+([^\ ]*) http://www.example.com/$1 [R=301,L]

bramley

10:00 pm on Apr 5, 2011 (gmt 0)

10+ Year Member



Actually there are two types of badly made links that I think i could make count :

www.example.com/page.html Read this

www.example.com/page.htmlMore

Same thing but without the space

bramley

10:03 pm on Apr 5, 2011 (gmt 0)

10+ Year Member




RewriteRule ^([^\ ]+)[\ ]+([^\ ]*) http://www.example.com/$1 [R=301,L]

That was fast! And much more elegant :)

Thanks g1smd

g1smd

10:06 pm on Apr 5, 2011 (gmt 0)

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



If all the URLs to be fixed end .html then this might work:

RewriteRule ^(([^/]+/)*[^.]+\.html)([\ ]+)?[^\ ]+ http://www.example.com/$1 [R=301,L]

bramley

10:14 pm on Apr 5, 2011 (gmt 0)

10+ Year Member



What does the ? do in that example ?

wilderness

10:31 pm on Apr 5, 2011 (gmt 0)

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



from the forum library and Mod_Rewrite and Regular Expressions [webmasterworld.com]

? matches 0 or 1 of the characters or set of characters in brackets or parentheses immediately before it.

bramley

10:36 pm on Apr 5, 2011 (gmt 0)

10+ Year Member



wilderness - thanks, that's a nice resource. I'll have to study it as one can do cool things with this tech :)