# Easy way to add .html extension to all URLs?

**URL:** <https://www.ruby-forum.com/t/easy-way-to-add-html-extension-to-all-urls/142987>\
**Category:** Rails\
**Created:** [July 14, 2008, 9:13am UTC](https://www.ruby-forum.com/t/easy-way-to-add-html-extension-to-all-urls/142987 "2008-07-14T09:13:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [July 14, 2008, 9:13am UTC](https://www.ruby-forum.com/t/easy-way-to-add-html-extension-to-all-urls/142987/1 "2008-07-14T09:13:29Z")

</div>

Is there an easy way to add a .html extension to the end of all URLs?

I’ve tried adding this to routes.rb:  
map.connect ‘:controller/:action/:id.html’, :format =\> ‘html’

or this to the ApplicationController:  
def default\_url\_options(options)  
{ :format =\> ‘html’ }  
end

but neither of these methods renders the right link with the link\_to  
tag \<%= link\_to ‘Show’, post] %\>

Do I have to use or is there a  
more elegant solution?

Thanks!

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [July 14, 2008, 10:52am UTC](https://www.ruby-forum.com/t/easy-way-to-add-html-extension-to-all-urls/142987/2 "2008-07-14T10:52:44Z")

</div>

et wrote:

> Is there an easy way to add a .html extension to the end of all URLs?
> 
> I’ve tried adding this to routes.rb:  
> map.connect ‘:controller/:action/:id.html’, :format =\> ‘html’

> but neither of these methods renders the right link with the link\_to  
> tag \<%= link\_to ‘Show’, post] %\>
> 
> Thanks!

Where did you put that extra route?

Routes are matched in the order they feature in your routes.rb file

so,

map.admin ‘admin/:action/:id.html’, :controller =\> ‘admin’, :format =\>  
“html”  
…

map.connect ‘:controller/:action/:id.html’, :format =\> “html”  
map.connect ‘:controller/:action.html’, :format =\> “html”

# now the defaults

map.connect ‘:controller/:action/:id.:format’  
map.connect ':controller/:action/:id

should generate with .html for you,  
but why do you want this?

by all means provide a legacy fallback to .html,  
but moving forward,  
why is it good to depend on this extension?

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [July 14, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/easy-way-to-add-html-extension-to-all-urls/142987/3 "2008-07-14T17:49:04Z")

</div>

I have the extra route above the default ones, but is there a way to  
generate “/posts/1.html” using \<%= link\_to ‘Show’, post %\> or a  
simpler method than what I have above?

Right now /posts/1.html works if I navigate directly to it, but the  
links on the views/posts/index.html.erb are still rendering as /posts/  
1

The reason I want the extension is to make sure search engines will  
see /posts/1.html as a document in the first level directory as  
opposed to /posts/1 as a second deeper level directory.

On Jul 14, 4:52 am, Matthew R. Jacobs \<rails-mailing-l…@andreas-
