Welcome Guest, Not a member yet? Register   Sign In
ciHTACCESS the best .htaccess for CodeIgniter
#1

[eluser]kuroir[/eluser]
After a lot of time dealing with CodeIgniter I figured out that one of the main problems with it is the duplicated content that by accident you can end up with.

Lets say we're using a clean installation of CodeIgniter. This means we have the default_controller set to "welcome". Lets suppose we use the easy .htaccess files around the web that just remove the "index.php" file.

So... we have the following "alternatives" to access to our 'default' controller.

Code:
http://test.com/
http://test.com/welcome
http://test.com/welcome/
http://test.com/welcome/index
http://test.com/welcome/index/

And if you have a URL suffix, for instance ".html" you'll be able to access on more ways:
Code:
http://exolimpo.com/welcome.html
http://exolimpo.com/welcome/.html
http://exolimpo.com/welcome/index.html
http://exolimpo.com/welcome/index/.html

You can even do some crazy stuff like:
Code:
http://test.com/welcome///////////////index//////////.html

Every single variant is a different page for Search Engines.. so this is bad for us. But then: What do we need to do?

Code:
1. Make the 'default' controller and index.php point to "/"
2. Remove the /index/ segment from the URL.
3. Remove Trailing Slashes
4. Remove Multiple Slashes in between url.
5. Add the url sufix (optional)
6. Remove slashes before the url suffix (optional)

After some test I got the following:

Code:
<IfModule mod_rewrite.c>

# ciHTACCESS, by Mario "Kuroir" Ricalde
RewriteEngine On
RewriteBase /

# Redirect index.php and default controller (you need to edit this) to "/". This is to prevent duplicated
# Content. ( /welcome/index , index.php => /)
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]

# Remove /index/ segment on the URL.
RewriteRule ^(.*)/index/? $1 [L,R=301]

# Remove Trailing Slashes.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)(/+)$ $1 [L,R=301]

# Remove Multiple slashes in betweeen
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

# Add the file SUFIX (sufix can be set on config.php).
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !\.html
# RewriteRule ^(.+)$ $1\.html [L,R=301]

# Remove any slash before .html ( to prevent site/.html )
# RewriteCond %{REQUEST_URI} \/+\.html$ [NC]
# RewriteRule ^(.+)\/+\.html$ $1\.html [L,R=301]

# Send everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

Test it out and tell me if you find any bugs. Just save it into your .htaccess Smile

Any comment would really help.. I'm wondering if anyone needs this?


Messages In This Thread
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 10-20-2009, 07:23 PM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 10-21-2009, 01:10 AM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 10-21-2009, 02:05 AM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 10-21-2009, 04:57 AM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 10-21-2009, 05:00 AM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 09-26-2010, 10:27 AM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 09-26-2010, 07:13 PM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 07-03-2011, 02:04 PM
ciHTACCESS the best .htaccess for CodeIgniter - by El Forum - 11-23-2012, 11:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB