Welcome Guest, Not a member yet? Register   Sign In
Ultimate .htaccess
#1

[eluser]voltechs[/eluser]
Heh, that probably got your attention! :lol:

Hey Guys, I've been struggling with .htaccess for quite while. I can't imagine that I was the only one with my requirements, but I couldn't find any exact solutions. I found a lot of help, but those never fully did what I wanted. Anyway, surprisingly this post isn't specifically asking for help, I thought I'd post my findings to help out anyone who I'm sure will find themselves in a similar situation.

My requirements were:
* No index.php anywhere in the URI
* Trailing slash always e.g http://example.com/products/ *
* Fall-through redirection if its actually a file.
* DON'T fall-through if its a directory. (I have a directory called "files" and a section called /files/)
* http://example.com/ should go to default controller.

Some of these requirements seem easy, but it took awhile to orchestrate them together. My first rewrite rule you might not want so you can get rid of that if you want. Also, be sure to check out some of the other posts about .htaccess on this board. There are various solutions to other scenarios worth checking out. For example, starting from a sub-directory other then "/". I'll reference some posts at the bottom.

So when you finally look at the solution, it looks really simple, but man, maybe I'm an idiot, but it took a while to get there. I had to try all kinds of combinations of different configurations. Hopefully I've done all the hard work for you! :lol: I'm using Code Igniter 1.5.4, PHP5, and I'm hosted by media temple (mt). I'm on their (gs) plan. Just for completion, I'll include some of my other settings just incase, routes probably won't be significant for you but here it is anyway;

.htaccess
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #remove www. from the uri and redirect with a 301 code.
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^example\.com
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

    #rewrite uri if its not a file to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    #putting '.+' versus '.*' allows it to fall through for index.php
    #i'm using /index.php/$1 because otherwise some pages would
    #insert a ? at the end of the URI and then repeat it, (try using
    #RewriteRule ^(.+)$ /index.php?$1
    #to see what I mean)
    RewriteRule ^(.+)$ /index.php/$1
    
</IfModule>

config.php
Code:
$config['base_url']    = "http://example.com/";
$config['index_page'] = "";

routes.php
Code:
$route['default_controller'] = "home";
$route['about'] = "home/about";
$route['resume'] = "home/resume";
$route['post/:num'] = "home/post";
$route['post/comment/:num'] = "home/post_comment";
$route['delete/(.*)/:num'] = "admin/delete_$1";
$route['update/(.*)/:num'] = "admin/update_$1";

$route['scaffolding_trigger'] = "";

I also was searching for a site which told me what all those cryptic "[R=301,L]" stuff meant. Even to a seasoned programmer some of that stuff wasn't very obvious. Once you have some explanations though, and some examples you should be fine picking most of it up. http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html. Sub-pages of that site I'm sure would be helpful too. I'll leave it to you to explore from there.

* http://ellislab.com/forums/viewthread/62349/
* http://ellislab.com/forums/viewthread/63746/
* http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
* (more specifically) http://httpd.apache.org/docs/1.3/mod/mod...ewriteCond

Anyway, hope it helps! Later guys. Good Luck.

-- Robin

EDIT: * Ok correction. My htaccess doesn't account for a trailing slash, but all the pages seem to work regardless. I suspect if you need a trailing slash, you could add a "/" after the index.php/$1;

Code:
RewriteRule ^(.+)$ /index.php/$1/


Messages In This Thread
Ultimate .htaccess - by El Forum - 11-09-2007, 05:12 PM
Ultimate .htaccess - by El Forum - 11-29-2007, 12:01 AM
Ultimate .htaccess - by El Forum - 11-29-2007, 10:13 AM
Ultimate .htaccess - by El Forum - 12-02-2007, 09:00 PM
Ultimate .htaccess - by El Forum - 12-07-2007, 05:23 AM
Ultimate .htaccess - by El Forum - 12-09-2009, 08:13 AM
Ultimate .htaccess - by El Forum - 12-03-2010, 10:58 AM
Ultimate .htaccess - by El Forum - 12-03-2010, 07:13 PM
Ultimate .htaccess - by El Forum - 01-04-2011, 01:54 AM
Ultimate .htaccess - by El Forum - 01-12-2011, 09:13 PM
Ultimate .htaccess - by El Forum - 01-13-2011, 02:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB