CodeIgniter Forums

Full Version: htaccess and canonicalization
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hi - I am trying to remove index.php from the Root url i.e. so that...
... resolves to

I have tried to find the answer in the other posts but don't seem to manage to find it.

My current htaccess file is this...

RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_FILENAME} !rss2html.php
RewriteCond %{REQUEST_FILENAME} !blog/(.*)

RewriteRule (.*)\.php$ index.php?/main/$1

RewriteRule ^admin/main(/)?$ index.php?/admin [NC,L]
RewriteRule ^admin/main/(.+)$ index.php?/admin/$1 [NC,L]

RewriteRule ^admin/contacts(/)?$ index.php?/contacts [NC,L]
RewriteRule ^admin/contacts/(.+)$ index.php?/contacts/$1 [NC,L]

RewriteRule ^admin/marketing(/)?$ index.php?/marketing [NC,L]
RewriteRule ^admin/marketing/(.+)$ index.php?/marketing/$1 [NC,L]

RewriteRule ^admin/site(/)?$ index.php?/site [NC,L]
RewriteRule ^admin/site/(.+)$ index.php?/site/$1 [NC,L]

RewriteRule ^admin/articles(/)?$ index.php?/articles [NC,L]
RewriteRule ^admin/articles/(.+)$ index.php?/articles/$1 [NC,L]

RewriteRule ^admin/resumes(/)?$ index.php?/resumes [NC,L]
RewriteRule ^admin/resumes/(.+)$ index.php?/resumes/$1 [NC,L]

RewriteRule ^admin/papers(/)?$ index.php?/papers [NC,L]
RewriteRule ^admin/papers/(.+)$ index.php?/papers/$1 [NC,L]

# Redirect to remove trailing period or comma from URL request
# with parameters, such as from forum with autolink, and force
# www to always be in the URL:
RewriteCond %{QUERY_STRING} ^(([^&]+&)*)[.,]$
RewriteRule (.*)$1?%1 [R=301,L]

# Redirect to remove trailing period or comma from URL request
# with path, such as from forum with autolink, and force www:
RewriteRule ^(([^/]+/)*)[.,]$$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^
RewriteRule ^(.*)$$1 [R=301,L]

In the files which I have set up I have set the name of the homepage to be "index".
If I remove this altogether I get...

Also in config.php. I have tried both
$config['index_page'] = "";
$config['index_page'] = "index.php";

Hope one of you experts can advise if this is possible. .


El Forum

[eluser]Daniel Moore[/eluser]
I have no idea why you're trying to rewrite "admin/main", "admin/contacts", and all those other directories. If these are your controllers and functions, then you are going about this all wrong.

The following code is well commented so that you can customize it to your needs.

Use the following:
#  Code Igniter .htaccess file for the main index.php directory

#  If you must have your CI "system" directory at,
#  Then I advise you to place a .htaccess file in that directory
#  with "deny from all" in it.

# Restore "allow from all" / I use a "deny from all" in some directory structures,
# especially where I am hosting multiple sub-domains that point to subdirectories
# of the main domain, this restores it.
allow from all

# Turn off directory listings for increased security
Options -Indexes

Options +FollowSymLinks

# Some web servers require this for index.php to be removed properly.  You may try
# your .htaccess with or without the following line.  Most work fine with it.
Options -MultiViews

# Make index.php the directory index page
DirectoryIndex index.php

# Turn on the RewriteEngine and set the base and condtions
RewriteEngine on
RewriteBase /

# The following 2 lines will force a www.prefix.  If you don't want this,
# then comment out these 2 lines.  Be sure to replace ""
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

# The following lines begin the conditional setup for removing index.php
RewriteCond $1 !^(index\.php|public|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#  use the following line if CI is in the root of the URL.
#  You may need to use index.php/$1 or index.php?/$1
#  Try it with or without the ? after index.php to see which works for you.
#  GoDaddy accounts require the ?
#  My localhost with XAMPP on Windows also requires the ?
#  Most hosts work correctly without the ?, so try it without first unless
#  you know your host requires it.
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

#  use the following if CI is in a sub-directory, and place this
#  file IN that subdirectory.
#  Replace '<directory_name>' with the name of the sub-directory.
#  Use this if you have an application at
#RewriteRule ^(.*)$ <directory_name>/index.php?/$1 [L,QSA]

Open up your application/config.php and find the line that contains "$config['uri_protocol'] =". Check the value.

Try each of the values listed in the comment above it one at a time ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') and see if that fixes the problem.

Also make sure that config.php file has
$config['index_page'] = "";

El Forum

Thanks a lot for your help Daniel. I will have a look at your recommendaitons and see if I can get it to work. Actually another guy developed this for me but I am sure he had some good reason for doing it this way as I had requested to have seo friendly urls. Thanks again.

El Forum

@thewebseye: Yeah, your last programmer put together something that worked, but Daniel's code does the same thing in a much cleaner package. Hopefully you got everything up and running...

@Daniel: Thanks for the tidy .htaccess file. I used it to take care of a few things I forgot. :-)

El Forum

[eluser]Daniel Moore[/eluser]
Your quite welcome, Torus.

El Forum

[eluser]Walter Coots[/eluser]
Yeah, thanks for this. Excellent, clean and comprehensive handling across multiple types of server configurations. I love it!