Redirecting old URLs to new permalinked URLs

Im currently in the process of adding permalinking to my site; at the
moment I am simply testing it out in development mode.

The problem is that at the moment I have all of these URLs that lead
to the same content:

http://127.0.0.1:3000/categories/1-css/tutorials/12-test9
http://127.0.0.1:3000/tutorials/12-test9
http://127.0.0.1:3000/categories/1/tutorials/12
http://127.0.0.1:3000/tutorials/12

What I'm trying to acheive is to get all of these URLs to redirect to
the top one so that google won't think I've got duplicate content.

Please Help,

If you need any more information feel free to ask,

Thanks In Advance,

-Joe

Bump?! Can anyone help with this?

Try gem rack-rewrite, that can solve your problem.

Cheers,
Lecky

I see code like this used in examples:

config.gem 'rack-rewrite', '~> 1.0.0'
  require 'rack/rewrite'
  config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
    rewrite '/wiki/John_Trupiano', '/john'
    r301 '/wiki/Yair_Flicker', '/yair'
    r302 '/wiki/Greg_Jastrab', '/greg'
    r301 %r{/wiki/(\w+)_\w+}, '/$1'
  end

Where does this code go?

How can I make this apply to a whole group? For example instead of
only making tutorials/61 go to the proper URL, making anything that is
just "/tutorials/SOMETHING" go to the proper URL?

Please Help,

Thanks In Advance,

-Joe

I see code like this used in examples:

config.gem 'rack-rewrite', '~> 1.0.0'
  require 'rack/rewrite'
  config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
    rewrite '/wiki/John_Trupiano', '/john'
    r301 '/wiki/Yair_Flicker', '/yair'
    r302 '/wiki/Greg_Jastrab', '/greg'
    r301 %r{/wiki/(\w+)_\w+}, '/$1'
  end

Where does this code go?

How can I make this apply to a whole group? For example instead of
only making tutorials/61 go to the proper URL, making anything that is
just "/tutorials/SOMETHING" go to the proper URL?

Please Help,

Thanks In Advance,

-Joe

Hi Joe,

Firstly, this code goes in config/application.rb. Secondly, you can
write regular expression like the example did to redirect your URLs.

Cheers,
Lecky

More problems..

Firstly I don't have an application.rb file to add this into.

Secondly, where does my example use a regular expression? and how can
I do something similar with my RoR application?

Bump?! Please help.

Bump?! Please Help

I see code like this used in examples:

config.gem 'rack-rewrite', '~> 1.0.0'
require 'rack/rewrite'
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
rewrite '/wiki/John_Trupiano', '/john'
r301 '/wiki/Yair_Flicker', '/yair'
r302 '/wiki/Greg_Jastrab', '/greg'
r301 %r{/wiki/(\w+)_\w+}, '/$1'
end

Where does this code go?

Look at your app and see where you find 'config.gem' entries. Or
read about using Rack middleware. It should be pretty obvious :slight_smile:

How can I make this apply to a whole group?

And you've already gotten that answer -- use a regular expression.
If you don't recognize that your own example includes one, you're
probably in deep bandini already :slight_smile:

Good luck,

The environment files?

I think thats where it would go. As for regular expressions, I don't
have a clue where it is in the example, Im really not amazingly good
in RoR and I would very much appreciate some help with figuring it
out..

Thanks In Advance,

-Joe

The environment files?

I think thats where it would go.

So try it and see :slight_smile:

As for regular expressions, I don't
have a clue where it is in the example, Im really not amazingly good
in RoR

What programming language *do* you know? Because if this line:

    r301 %r{/wiki/(\w+)_\w+}, '/$1'

:: doesn't jump out at you as a regular expression -- certainly when
contrasted with the other example lines -- then I suspect you need to
just plain read up on RegEx in general...

I program in many languages, and that does not stand out to me.
I know C++, Java, CSS, HTML, Lua, and Visual Basic; and that doesn't
stand out to me. I assume that this is from a whole different category
of programming/scripting languages; of which I don't know.

Could you please explain to me how it works, and how I can use it in
my personal scenario?

Thanks In Advance,

Joe

unknown wrote:

I program in many languages, and that does not stand out to me.
I know C++, Java, CSS, HTML, Lua, and Visual Basic; and that doesn't
stand out to me.

CSS and HTML aren't programming languages. C++ and Java don't have much
regex support. I don't know Lua or VB, so can't comment.

I assume that this is from a whole different category
of programming/scripting languages; of which I don't know.

To some extent.

Could you please explain to me how it works, and how I can use it in
my personal scenario?

That would be a bit of a waste of time until you go read the Pickaxe
Book's explanation of regular expressions. You've asked a huge number
of elementary questions here that could have been answered by reading
the docs.

Thanks In Advance,

Joe

Best,

I think you need to just learn something about regular expressions.

   /wiki/(\w+)_\w+

"\w A word character: [a-zA-Z_0-9]"

The above definition is taken from the API doc for `java.util.regex` :slight_smile:
which also uses the same '+' quantifier and '( )' capture group syntax.

So there's nothing out of the ordinary here that a little regex study
shouldn't serve to enlighten...

Hmm. Ok. Ill look into it.

So I guess ill want something like this: r301 %r{/tutorials/(\w+)}, '/
$1'
But instead of using the first unknown word variable (as "/$1" would
refer to (as far as I know)); I want it to go to my permalinked URL
(http://127.0.0.1:3000/categories/1-css/tutorials/12-test9 ).

Please Help (Meanwhile ill look up some regex),

Thanks In Advance,

Joe

Why guess? That's what docs are for --

<http://github.com/jtrupiano/rack-rewrite/blob/master/README.rdoc>

There's this thing called 'Der Google', see... :slight_smile:

I've already read this, and still don't understand.

Would I need something like this?

rewrite %r{/tutorials/(\w+)categories/(\w+)}, '/categories/
CategoryPermalinkHere/tutorials/TutorialPermalinkHere'
rewrite %r{/tutorials/(\w+)}, '/categories/CategoryPermalinkHere/
tutorials/TutorialPermalinkHere'

Please Help,

I've looked everywhere but just can't figure it out.

Thanks In Advance,

-Joe

Hassan Schroeder wrote:

As I said, it's using regular expressions, and *you* need to figure out
how they work. Find a regex tutorial, open up irb and play around.

Then, or even before, use the examples in the Rack::Rewrite docs to
experiment. It'll take less time to *just try it* than to compose an email.
And at least you'll be able to say "here's what I want, here's what I've
tried, here are the results".

FWIW,

I've tried, but I can't figure out how to replace $1 with a permalink
variable thats in ruby on rails. Im pretty sure regex cannot help me
with this, I need to take the permalink value, and use that in the
redirect.

PLEASE JUST TELL ME AND ILL LEARN, I DONT JUST TAKE THE ANSWER AND BE
DONE WITH IT, I LEARN FROM IT.

Please Help,

Thanks In Advnce,

Joe