CodeIgniter Forums
Eliminating the index segment in a URL (htaccess) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Eliminating the index segment in a URL (htaccess) (/thread-32751.html)



Eliminating the index segment in a URL (htaccess) - El Forum - 08-03-2010

[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/building-a-basic-php-e-commerce-application-using-codeigniter-framework-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


Eliminating the index segment in a URL (htaccess) - El Forum - 08-03-2010

[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


Eliminating the index segment in a URL (htaccess) - El Forum - 08-03-2010

[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?


Eliminating the index segment in a URL (htaccess) - El Forum - 08-03-2010

[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.


Eliminating the index segment in a URL (htaccess) - El Forum - 08-03-2010

[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


Eliminating the index segment in a URL (htaccess) - El Forum - 08-04-2010

[eluser]cseguin[/eluser]
Hi tim042829,

I have a similar problem,

any luck?

we are waiting for you to report back :-)

regards


Eliminating the index segment in a URL (htaccess) - El Forum - 08-04-2010

[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.


Eliminating the index segment in a URL (htaccess) - El Forum - 08-04-2010

[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


Eliminating the index segment in a URL (htaccess) - El Forum - 08-07-2010

[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