Welcome Guest, Not a member yet? Register   Sign In
Requiring trailing slash at end of URL
#1

[eluser]jplanet[/eluser]
I have come to find out that it is good practice for SEO purposes to have URL's which will only respond to one precise format. With Codeigniter, I am finding that I can always add a slash to the end of the URL so that:

www.example.com/controller/function

can also be successfully requested with:

www.example.com/controller/function/

For other reasons, I would very much like to require that the last slash IS always present, so that if someone requests the URL, it will transparently perform a 301 redirect to include the trailing slash, similar to what happens when you navigate to a URL which is a directory root.

Is this best done in the routes.php, htaccess, or with a custom function?
#2

[eluser]tonanbarbarian[/eluser]
One option would be to enable the URL Suffix in the config. Set it to .html and then if you use the site_url function from the URL helper it will always add .html to the end of your urls.

so www.example.com/controller/function would be www.example.com/controller/function.html
and some more specific examples

www.example.com/users/index - list users
www.example.com/users/index.html

www.example.com/users/index/50 - paginated user list starting at record 50
www.example.com/users/index/50.html

www.example.com/users/edit/613 - edits user 613
www.example.com/users/edit/613.html

www.example.com/users/index/0/sort/login - paginated user list starting at 0 sorted by login date
www.example.com/users/index/0/sort/login.html

As you can see these urls are then very SEO friendly

Now technically I think the user would probably be able to type in the following and it would still work
www.example.com/users/index/0/sort/login.html/
but that is only because of the rewrite rules in the .htaccess, and as long as you ensure that all urls you create are done so via site_url you will find they will have the extension

Code:
<a href="&lt;?php echo site_url('users/index/0/sort/login'); ?&gt;">Login Sort</a>
&lt;?php anchor('users/index/0/sort/login', 'Login Sort'); ?&gt;
Both of these will product a link as follows
Login Sort
Because anchor uses site_url and therefore will add the .html extension to the url

Maybe this is not quite what you want but it is a viable alternative
#3

[eluser]Phil Sturgeon[/eluser]
If you use the CI helpers to create your urls there will never be a trailing slash. These include form_open, anchor, site_url, etc. All used properly as posted above then you will never have a / at the end.

One way to make sure there are ALWAYS slashes at the end is to use / as the url suffix. If you make sure you are using CI to generate url's then it will always be at the end and never double up.

Code:
$config['url_suffix'] = "/";
#4

[eluser]jplanet[/eluser]
Thanks for these responses...there is something specific about this though that might require something different, or perhaps just a variation on what has been suggested...

As part of the SEO plan for this website, I need to migrate URL's gradually. To do this, I have created directories on the old site (which was actually done in Cold Fusion on a Windows server). These directories correspond to the new CI site's URL's. For example,

www.example.com/gallery/

Contains an index.cfm file which shows the contents of what was once:

www.example/com/index.cfm?fuseaction=gallery

Now, the URL directly above, containing the query string, has a 301 redirect to the new /gallery/ url. Te idea is that search engines will have indexed the new URL's a few at a time before the domain is transferred to the new CI site.

What the suggestions above do not address is, CI URL's always work with or without the slash at the end. For this very page we are reading now, http://ellislab.com/forums/viewthread/67843/, also works as http://ellislab.com/forums/viewthread/67843. This is a problem in SEO...I would have to find a way to force http://ellislab.com/forums/viewthread/67843 to 301 redirect to http://ellislab.com/forums/viewthread/67843/.




Theme © iAndrew 2016 - Forum software by © MyBB