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/
#2

[eluser]kith kanan[/eluser]
Hi!

I just tested your code but i can't make it workSad I'm a newbie in CI. Your help is highly appreciated.

By the way I'm running it on localhost and my root folder is "my-folder"

Here's my .htaccess code, i saved it in "my-folder" directory.


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} !^localhost/my-folder/
    RewriteRule ^(.*)$ http://localhost/my-folder/$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>

route.php

Code:
$route['default_controller'] = "main";
$route['scaffolding_trigger'] = "";

config.php

Code:
$config['base_url']    = "http://localhost/my-folder/";
$config['index_page'] = "";

And everytime i go to "http://localhost/my-folder/" it redirects me back to localhost. Am I missing something?

Thanks!
#3

[eluser]voltechs[/eluser]
Hey Kith.

I've got to head to work right now, but I just wanted to let you know I'm looking into it. Keep trying things out. My first thought is about the re-write base part of the .htaccess. Might need to change that. I don't know, but I'm gonna experiment when I get home, so keep trying things.

Talk to you later.

-- Robin

Well, I've been playing around, and I don't think I can replicate your environment properly. I say keep trying at it, read some of the other posts I've linked too. I'm gonna keep trying some things, but don't count on anything. Sorry to disappoint. I'm sure you'll come through though!
#4

[eluser]kith kanan[/eluser]
hi voltechs,

Thanks for your reply!Smile

But until now I can't make things work *sigh*

But thanks for your response anyway Smile

Regards,
kith
#5

[eluser]vlad22[/eluser]
dude, great job ! this is indeed ultimate Smile)
#6

[eluser]seismic[/eluser]
I have qeustion. Anyway so how the link <a href="..."> to the content should be look like in the menu?
http://www.example.com/index.php/article01
or
http://www.example.com/article01

maybe any other idea?
#7

[eluser]ephlodur[/eluser]
Hi all.

I have look at most of the response on this topic but none of them help.
Here is my configuration:\
I'm running Ubuntu with Apache2 and the http server is pointing the my codeigniter diretory
inside that directory I have mysite I have the below files/directory
application
/css
.htaccess
/img
index.php
js
license.txt
/system
/user_guide
with the config as below
$config['base_url'] = "http://example.com";
$config['index_page'] = "";

for now all will work only if I add the index.php in the config

below is my .htaccess file
RewriteEngine on
RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|image|library|style|script|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]


Thanks for your help
#8

[eluser]LifeSteala[/eluser]
[quote author="kith kanan" date="1196337670"]Hi!

I just tested your code but i can't make it workSad I'm a newbie in CI. Your help is highly appreciated.

By the way I'm running it on localhost and my root folder is "my-folder"

Here's my .htaccess code, i saved it in "my-folder" directory.


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} !^localhost/my-folder/
    RewriteRule ^(.*)$ http://localhost/my-folder/$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>

route.php

Code:
$route['default_controller'] = "main";
$route['scaffolding_trigger'] = "";

config.php

Code:
$config['base_url']    = "http://localhost/my-folder/";
$config['index_page'] = "";

And everytime i go to "http://localhost/my-folder/" it redirects me back to localhost. Am I missing something?

Thanks![/quote]

Try:

Code:
RewriteBase /my-folder/
#9

[eluser]PEN Vannak[/eluser]
Hi!

Can you try this?
$config['rewrite_short_tags'] = TRUE;
Place it in system/application/config/config.php

Thanks,
#10

[eluser]PEN Vannak[/eluser]
Hi guy!,

I just fond it from other forum how to remove index.php from your url.

Please visit this link to see it http://www.anmsaiful.net/blog/php/remove...x-php.html.

I tried it and it works.




Theme © iAndrew 2016 - Forum software by © MyBB