How does Stack Overflow generate its SEO-friendly URLs?

后端 未结 21 2584
-上瘾入骨i
-上瘾入骨i 2020-11-22 04:27

What is a good complete regular expression or some other process that would take the title:

How do you change a title to be part of the URL like Stack

21条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 04:53

    You will want to setup a custom route to point the URL to the controller that will handle it. Since you are using Ruby on Rails, here is an introduction in using their routing engine.

    In Ruby, you will need a regular expression like you already know and here is the regular expression to use:

    def permalink_for(str)
        str.gsub(/[^\w\/]|[!\(\)\.]+/, ' ').strip.downcase.gsub(/\ +/, '-')
    end
    

提交回复
热议问题