Welcome Guest, Not a member yet? Register   Sign In
Proper Section Structure for CI Projects? (getting rid of the index.php in the pathing)
#1

[eluser]Unknown[/eluser]
Hello all,
This is an embarrassingly dumb question, so I'm hoping it might be an easy one. CodeIgniter is my first real php framework, so I'm fairly new to the paradigm. I went through the two tutorials and have been going through the documentation and have yet to find an answer to what seems like a farily simple question...

I have my app/site hosted on my local box, let's call it 'sitename' for now. The pathing is such:
http://localhost/websites/sitename/

My index page comes up fine using this pathing, so i know all my settings for index.php as the default page are correct (e.g. sitename/ is the same as sitename/index.php). But let's say I have another page, called "about". I have created the "about" function, also tried just creating a separate controller file. However, this "about" page is only viewable when I hit:
http://localhost/websites/sitename/index.php/about

... but I'd like to have it work as follows: http://localhost/websites/sitename/about

Essentially, I'm looking to build my site with out the "index.php" sandwiched in there... I am thinking this may have something to do with my .htaccess file, but I'm stumped? Is there a doc that outlines good practices for paging your CI project?


Thanks in advance!
#2

[eluser]Pascal Kriete[/eluser]
Welcome to Codeigniter noctipx Smile ,

It does indeed have something to do with you .htaccess file (if you're running apache anyways).

Here's the problem: An old-school request has a url that looks like the path on the server. If you requested /flying/aardvark.html then there was a flying folder and a file called aardvark.html . CodeIgniter, as well as a lot of other frameworks, pass all requests through one file (index.php), and work out the path themselves. That's a problem when the server's natural reaction is to try and find a filesystem path.

What a proper htaccess file will do, is tell your webserver to change all requests that fit some sort of pattern into something else. That is done internally, and is what gives mod_rewrite it's name. So really, you're just masking the actual url, the server still puts the index.php there.

That's the theory, now how to do it. A basic htaccess file can be found in the user guide. However, that won't work if your application is in a subdirectory (because of the leading slash in the rewrite rule).

I usually change that last line, so the file reads:
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Quick explanation: RewriteCond - in this case, a list of the paths that should be ignored, RewriteRule - everything else is prepended with index.php/

Give that a shot, place it in the root folder of your application ('next to' index.php) and let us know how it goes.
#3

[eluser]tomcode[/eluser]
Do a search in the forum and wiki for rewrite or htaccess and You'll find plenty of examples.

On apache.org You'll find the reference docs and tutorials, search 'rewrite'. Watch out for Your servers apache version.

I found that some servers need RewriteBase set, others don't.

With phpinfo() You'll know whether mod_rewrite is installed, on Your local server You load it by changing the corresponding line in httpd.conf (make a copy first).
#4

[eluser]Unknown[/eluser]
Fantastic - that did the trick. Thanks to both of you, inparo and tomcode, I now have my site working the way I need it to.

Since my localhost is an OS X (10.5 client) machine, I had to enable .htaccess files in my apache settings by doing the following:
1. cd /private/etc/apache2
2. sudo mate httpd.conf
3. In TextMate (or whatever texteditor you're using, change (on/around line 210): 'AllowOverride None' to 'AllowOverride All'
4. sudo apachectl restart

Now this .htacccess rewrite stuff seems to have affected my .css doc.. i'm going to have to look in to making an exception for files named .css.....

Thanks again!
#5

[eluser]Phil Sturgeon[/eluser]
The example rewrite posted above takes exceptions which will be ignored. You need to add any other files or folders you want to be ignored by CI to the rule.

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|somefile.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Alternatively, use the following rewrite rule as it does not require any exceptions. Instead it only sends URL's to CI if it cannot find them in the filesystem.

Code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

That should work, I may have removed a little too many rules as mine is quite packed with other things. Give it a shot ;-)




Theme © iAndrew 2016 - Forum software by © MyBB