1
votes

Hello all i have been working on a new project in Codeigniter and everything is working great except one thing that is url

What i want is to remove the controller name main and the word index.php from my URL like my url is right now like this

Current URL: www.domain.com/index.php/main/contact

where main = controller name and contact is function name

Needed url: www.domain.com/contact

i know that i can remove index.php and controller name by htaccess and i have also achieved it some what bt the problem is all of my links in the website is already lined as domain.com/index.php/main/contact so i want something which can automatically redirect the url to the required url without index.php and controller name.

Note: controller name is always "main"

My htaccess file is like this in root folder:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
</IfModule>

# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Header set Access-Control-Allow-Origin "*"

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

Any help in this manner is highly appreciated. also i do not want to have any problem with my form submitions as they have the action urls etc.

2
Be sure your .htaccess file should be in root dir. not in system folder also remove .htaccess file from application folder @Lisa - Klint
Give this a shot if others fail. This has always worked for me. Fool proof every time. Regardless of the framework. stackoverflow.com/a/35014499/3532758 - user3532758
i can remove the index.php from url but i want ot to be auto redirected too. - LIsa
With the current rules as shown, if you enter domain.com/contact in browser, does it load correct content? - anubhava

2 Answers

1
votes

Insert this rule just below RewriteEngine On line as your topmost rule:

# remove /index.php/main/ from URLs
RewriteCond %{THE_REQUEST} \s/+index\.php/main/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^index\.php/main(/.*)?$ https://%1$1 [L,NC,NE,R=301]
2
votes

1) To remove index.php from the URL you can follow this CodeIgniter removing index.php from url

2) To rewrite your url from www.domain.com/index.php/main/contact to www.domain.com/contact you have to do the routing in application->config->routes.php. Like this

$route['contact'] = 'main/contact';