Forum Moderators: phranque

Message Too Old, No Replies

Need a ReWrite Rule -- Help!

Looking for a rewrite rule creating?

         

navaho

1:18 pm on Aug 11, 2012 (gmt 0)

10+ Year Member



Would anyone know of a quick rewrite rule to handle the following 404 errors, there are +/- 300 of these on my site:-

www.thisismydomain.com/articles/i-need-help-with-a-rewrite-rule/1495/

Should redirect to:-

www.thisismydomain.com/i-need-help-with-a-rewrite-rule/

So the /articles/ section and the final /1495/ number need to be removed for the redirect?

Any guidance would be appreciated,

Many thanks

g1smd

2:00 pm on Aug 11, 2012 (gmt 0)

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



Let's see your best attempt at the code.

In order for this to work, you'll need the pages of your site to link to the URLs that you want users to see and use. URLs are defined in links.

navaho

2:16 pm on Aug 11, 2012 (gmt 0)

10+ Year Member



I Like your style g1smd, I like a challenge! The best I could do is :

redirect 301 /articles/(\w+)/(\w+)$ /$1

But that don't work!

g1smd

7:10 pm on Aug 11, 2012 (gmt 0)

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



You're on the right lines. You'll need a RewriteRule with [R=301,L] flags.

The RegEx pattern will need to capture the various parts for re-use and the target URL should include protocol and hostname.

Start anchor and end anchor the RegEx pattern to avoid ambiguity.

navaho

7:51 pm on Aug 11, 2012 (gmt 0)

10+ Year Member



Man, you just lost me...totally!

Any chance you can give me the code?

I am not up to scratch with Mod ReWrite...at all!

Thanks in anticpation of your help!

lucy24

11:36 pm on Aug 11, 2012 (gmt 0)

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



I'm surprised the example you gave didn't lead straight to a 500 error :) "Redirect" by that name is the "vanilla" version of mod_alias. It uses literal text, not Regular Expressions. If you need to use Regular Expressions in mod_alias you have to go to RedirectMatch. But here you almost certainly need mod_rewrite instead.

Problem #2 is that your Rule doesn't fit your example. If your article names really are in the form "i-need-help-with-a-rewrite-rule" with words separated by hyphens, then none of them will ever be matched by \w alone. You need [\w-]+

Since you are throwing away the last part of the URL (the numbers) you don't need to capture it in the first place. In fact, in mod_rewrite you don't even need to look at it. mod_alias reappends any leftover part of the URL; mod_rewrite throws everything away and starts from scratch.

Problem #0 is the most important however.
a: Where are your users getting the wrong URLs? (The ones you need to redirect from.)
b: What is the actual location of the content you're serving?

navaho

8:35 am on Aug 12, 2012 (gmt 0)

10+ Year Member



Thanks for coming in Lucy24' I appreciate your input.

a: These are primarily crawl errors after a site redesign. These wrong url's are reported in GWMT, I have been manually adding the redirects but after the first 100 or so I thought that there must be a rule to cover it.

b: home/public_html/assuredltd/ is tha actual location

I hope that this is the correct interpretation of your questions.

Thanks again

lucy24

9:53 am on Aug 12, 2012 (gmt 0)

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



after the first 100 or so I thought that there must be a rule to cover it

It depends on whether there's a pattern. For example:

old URL
www.example.com/stuff/otherstuff/morestuff.html
www.example.com/stuff/blahblah/morestuff.html
www.example.com/stuff/foobar/morestuff.html

new URL
www.example.com/morestuff/otherstuff.html
www.example.com/morestuff/blahblah.html
www.example.com/morestuff/foobar.html

That's a pattern that can be reduced to a Regular Expression:

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

Or

old URL
www.example.com/blue-widgets.html
www.example.com/red-widgets.html
www.example.com/green-widgets.html

new URL
www.example.com/widgets/blue.html
www.example.com/widgets/red.html
www.example.com/widgets/green.html

The pattern then is:

RewriteRule (\w+)-widgets\.html http://www.example.com/widgets/$1.html

Going back to your original post:

www.example.com/articles/{nameofarticle}/{meaninglessnumbers}/
change to
www.example.com/{nameofarticle}/

If it's in the config file, keep the leading slash before "articles". If it's in htaccess, remove it:

RewriteRule articles/([^/]+)/\d+/ http://www.example.com ... and you can work out the last part for yourself ;)

If the numbers are always part of the old URL, it is best to include them in the pattern even though you won't be reusing them. It helps confirm that you're redirecting the right stuff.

Except, er, is every one of your articles in a directory by itself? That seems a bit chaotic. If there is anything after the numbers, you have to deal with it. mod_rewrite won't just reappend the rest of the path. At a minimum, simply capture it and stick it onto the target as $2.

Oh, yes. When I asked about location, I meant: Is the actual filepath simply

home/domainname/nameofarticle.html

or is there rewriting going on in the background? If so you have to make sure you don't end up going around in circles.

navaho

11:23 am on Aug 12, 2012 (gmt 0)

10+ Year Member



Hi Lucy24

I owe you a beer! That worked perfectly thanks! I even managed to complete your string on first attempt.

I really appreciate your help with this, it has taken a huge weight off my plate!

How do I buy you a beer?

lucy24

10:13 pm on Aug 12, 2012 (gmt 0)

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



I'll spend a few minutes contemplating the icons at {site the moderators don't approve of}. They've got several variants on beer-drinking. Well, it's a German site ;)