Welcome Guest, Not a member yet? Register   Sign In
Force Backslash on all URLs with url_suffix and .htaccess
#1

[eluser]WhatTheFawk[/eluser]
Just posting this because I couldn't find any topic on using uri_suffix to enforce a backslash on all urls in a site, hope it helps someone. Also I want to know if there's any downside to this.

My goal was to have backslashes added to every URL. Unfortunately I ran into problems with forms while using .htaccess to do this. My .htaccess code is:

Code:
# Add trailing slash to all URI's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/sitename/$1/ [R=301,L]

This worked except for one thing. When I submit a form without a trailing slash, it redirected it to the proper URL, but because of that redirect the forms $_POST data was lost.

The problem I was having is that functions like site_url() remove the trailing slash; this means third party library forms were breaking because form_open() uses site_url() on the url entered, removing any trailing slashes and causing .htaccess to redirect on submit, which loses all the form data.

Config uri_suffix to the rescue!
"In your config/config.php file you can specify a suffix that will be added to all URLs generated by CodeIgniter"

Don't mind if I do! After setting url_suffix to '/' I don't have to worry whether a third party library uses the trailing backslash or not, forms work as expected because a backslash is forced into all urls going through form_open(). Saved me from having to go through every form in the third party libraries I'm using just to replace form_open() with actual HTML so the backslash would be kept. :X

I wanted to know were there any downsides to using a backslash in uri_suffix, also are there any other ways around this or is this just the way to do it?
#2

[eluser]Mackstar[/eluser]
On your form did you try url_encode??, I personally haven't tried it and don't know why you would want to. I probably wouldn't recommend it unless a lot of other people are doing this. I also believe different browsers may handle the URL differently.
#3

[eluser]WhatTheFawk[/eluser]
[quote author="Mackstar" date="1238563890"]On your form did you try url_encode??, I personally haven't tried it and don't know why you would want to. I probably wouldn't recommend it unless a lot of other people are doing this. I also believe different browsers may handle the URL differently.[/quote]

How would url_encode help? I might not have explained very well, I want to avoid using query strings.

Basically in other libraries people make for CI its common for them to use form_open($url), but form_open() uses site_url() on the $url which will remove any backslashes at the end, this was a problem for me.

When the backslash is missing from any url, then my .htaccess says "hey this has no backslash" and redirects to the page with a backslash included. But the $_POST data submitted from any form is lost because of the redirect.

I needed a way to add the backslash back in EVERY url used on the site, like in a href links on the page, so .htaccess wouldn't have to redirect because all links would be correct, with backslash included. I think url_suffix in config adds to all urls, so I had it add a '/' instead of .html or whatever, just trying to make sure it wont mess up anything else having that backslash in there. Big Grin
#4

[eluser]TheFuzzy0ne[/eluser]
Perhaps I'm missing something, but what's to stop users removing the trailing forward slash? Will that break your site? I still don't get the whole "trailing forward slash" thing.
#5

[eluser]WhatTheFawk[/eluser]
Hey, removing the final slash will 301 redirect them to the one with the slash, for example go to (http://www.google.com), it redirects to (http://www.google.com/). They do it because those are considered two different urls, so just like you should enforce www or non-www across your entire site, you should enforce backslash or non backslash.

I wasn't really having any problem at first, I was just posting to see if anyone had done this and ran into any issues with the other functions in Code Igniter. I did find one issue with this today, it's that site_url() which is used in redirect() will add url_suffix even if the uri has a query string in it:

With config, uri_suffix = ".html"
Code:
site_url('test.html/?x=1');
will output:

test.html/?x=1.html

I think it should output:

text.html/?x=1

Because site_url() is used in functions like redirect() and it shouldn't be adding anything to the end if there's a query string in there.

To fix it I made a application/libraries/My_config.php
Copied the site_url() function from system/libraries/config.php

For reference this is line 205 in system/libraries/config.php

After:
Code:
$suffix = ($this->item('url_suffix') == FALSE

Add:
Code:
|| ereg('[?.*]', $uri)

Just checks to see if there is a ? with something after it in the uri before adding the suffix.




Theme © iAndrew 2016 - Forum software by © MyBB