Welcome Guest, Not a member yet? Register   Sign In
Mod rewrite force trailing slashes & form_open() - site_url()
#1

[eluser]35mm[/eluser]
It's a good idea to use mod rewrite to force www. and trailing slashes on (or off) your URLs to avoid duplicating content as www.site.com/controller/method/ and www.site.com/controller/method and http://site.com/controller/method/ and http://site.com/conroller/method give four duplicates of the same content which the search engines will mark you down for.

However, after I set up the mod rewrite I noticed I could no longer login to the site and other post forms were failing too. It took me a while to figure out that CI was removing the trailing slash when using
Code:
form_open('controller/method/')
so the 301 redirect was blocking the post. I went in search of help and pretty much all I found was this old thread http://ellislab.com/forums/viewthread/110396

The method in that thread is undoubtedly handy if you have an existing site with loads of post forms, and have just added the mod rewrite, and need a quick fix. However, hacking CI's core in my opinion, should be a last resort and isn't an elegant solution unless it's a bug fix or absolutely necessary.

One alternative is to manually code your forms, but CI's form_open is very convenient and saves time especially if you are using CSRF. So I came up with a simple fix that I'll share here in case it helps anyone else who runs into this problem. The solution: while coding your app, simply add a hash after the trailing slash like so
Code:
form_open('controller/method/#')
and that will force CI to keep the trailing slash, and because the hash is only client side, CI will ignore it.




Theme © iAndrew 2016 - Forum software by © MyBB