CodeIgniter Forums

Full Version: Eliminating the index segment in a URL (htaccess)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]tim042829[/eluser]
FYI: I am an experienced web developer (reference http://ellislab.com/forums/viewthread/162794/)
but I am new to PHP and codeIgniter.
I am working in a linux environment.

See: http://blog.insicdesigns.com/2009/02/bui...rk-part-1/

With the following .htaccess:
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
I can request /documentroot/CodeIgniter/system/application/controllers/test.php as
http://localhost/CodeIgniter/index/test/
Is there a way to eliminate the '/index/' segment (path part) from
the URL above?

I'm a noob if not a boob when it comes to .htaccess.
TIA
Tim

El Forum

[eluser]bretticus[/eluser]
"test" is your controller file so your URL should be:

Code:
http://localhost/CodeIgniter/test/

htaccess will rewrite that internally as:

Code:
http://localhost/CodeIgniter/index.php/test/

according to your Rewrite code.

CodeIgniter will automatically call your index method/function in your test controller unless you specify otherwise.

Please note:

Code:
http://localhost/CodeIgniter/test/

and

Code:
http://localhost/CodeIgniter/test/index

are equivalent.

Anything that needs to work outside of the controller/method URL convention, can be handles via routes (no need to update htaccess.)

See Routing

El Forum

[eluser]tim042829[/eluser]
[quote author="bretticus" date="1280895398"]"test" is your controller file so your URL should be:

Code:
http://localhost/CodeIgniter/test/
[/quote]
Actually, the URL above is generating a 404 error
Quote:htaccess will rewrite that internally as:

Code:
http://localhost/CodeIgniter/index.php/test/

according to your Rewrite code.
Calling the previous URL is successful.
Quote:CodeIgniter will automatically call your index method/function in your test controller unless you specify otherwise.

Please note:

Code:
http://localhost/CodeIgniter/test/

and

Code:
http://localhost/CodeIgniter/test/index

are equivalent.
And that previous URL request is also generating a 404 error.
Quote:Anything that needs to work outside of the controller/method URL convention, can be handles via routes (no need to update htaccess.)

See Routing
I appreciate the response. With the 404 error from the http://localhost/CodeIgniter/test/ URL,
I would then guess that there is something wrong with my code or with the .htaccess
code, or in the apache configuration itself.
Any ideas where to look?

El Forum

[eluser]bretticus[/eluser]
Quote:I appreciate the response. With the 404 error from the http://localhost/CodeIgniter/test/ URL,
I would then guess that there is something wrong with my code or with the .htaccess
code, or in the apache configuration itself.
Any ideas where to look?

Yes, modify your system/application/config/config.php to remove index.php from your url. See details in the wiki.

El Forum

[eluser]tim042829[/eluser]
Thank Betticus, I had thought of config.php following my post.
I will look at wiki and report back (probably tomorrow).
regards
tim

El Forum

[eluser]cseguin[/eluser]
Hi tim042829,

I have a similar problem,

any luck?

we are waiting for you to report back :-)

regards

El Forum

[eluser]bretticus[/eluser]
Posting the same thing I responded to @cseguin with:
Quote:The following config item:
$config['index_page'] = "index.php";

...probably just facilitates calls to base_url() and anchor() etc.. Because mod_rewrite is rewriting URLs internally, the config change shouldn’t be a factor in getting 404 errors.

Are you absolutely certain you have mod rewrite enabled on your local machine? You can check with phpinfo().

Make sure you have mod_rewrite enabled locally.

El Forum

[eluser]tim042829[/eluser]
[quote author="cseguin" date="1280953714"]Hi tim042829,
I have a similar problem,
any luck?
we are waiting for you to report back :-)
regards[/quote]
No luck! sigh..
I followed the instructions at http://codeigniter.com/wiki/mod_rewrite/
to the best of my ability. Mode rewrite is enabled on my machine.
I changed the AllowOverride directives in my apache default and default-css files
form None to FileInfo.
I used the .htaccess file as specified in that page.
A couple of comments here: I've done things the 'hard way' for 14 years now.
I want php to be the 'easy way', and furthermore, I am reluctant to do any system-wide
mods to apache.
If it ain't easy, I would find another way.
And I did.
Just to test, I activated the following line in config.php to
Code:
$config['index_page'] = "akwebsoft.php";

and copied index.php as akwebsoft.php. Now I can request
Code:
http://localhost/CodeIgniter/akwebsoft/test
and get my test page. So I have overwritten 'index' and that works for me now.
Maybe I will revisit that issue when I have graduated from noobhood.
Sorry,
tim

El Forum

[eluser]tim042829[/eluser]
I believe that I have it working now. mod_rewrite seems to be
successfully implemented.
1)Set AllowOverride => all (instead of fileinfo)
2)Symlinked /etc/apache2/mods-available/rewrite.load to /etc/apache2/mods-enabled/rewrite.load

And it worked. That's ubuntu 10.04, I believe that (for instance) slackware setup is different.
cheers
tim