1

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.

LIsa
  • 65
  • 7
  • Please follow each step https://stackoverflow.com/questions/19183311/codeigniter-removing-index-php-from-url – Klint Nov 16 '19 at 06:53
  • Be sure your `.htaccess` file should be in root dir. not in system folder also remove `.htaccess` file from `application` folder @Lisa – Klint Nov 16 '19 at 06:55
  • Give this a shot if others fail. This has always worked for me. Fool proof every time. Regardless of the framework. https://stackoverflow.com/a/35014499/3532758 – user3532758 Nov 16 '19 at 06:58
  • i can remove the index.php from url but i want ot to be auto redirected too. – LIsa Nov 16 '19 at 07:46
  • CI has it's own rewrite router that may not always play nice with .htaccess rewrites. – anubhava Nov 16 '19 at 09:42
  • @anubhava hi i am very happy to receive your comment, i know the route.php in CI, but my main problem is that i have already added all links as domain.com/index.php/main/contact and i want the htaccess to redirect automatically to domain.com/contact – LIsa Nov 16 '19 at 11:38
  • 1
    With the current rules as shown, if you enter `domain.com/contact ` in browser, does it load correct content? – anubhava Nov 16 '19 at 14:48
  • @anubhava yes it does load it correctly . – LIsa Nov 18 '19 at 06:11

2 Answers2

2

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';
iamatstackoverflow
  • 402
  • 2
  • 4
  • 13
1

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This works perfectly fine , this is awesome i knew you will help me yo are the best . just one last question i have many urls to rewrite like this so should i just all the urls like $route['contact'] = 'main/contact'; in route.php or it can be automatic also ?? – LIsa Nov 19 '19 at 05:32
  • It can be automatic in a standard htacceas setup but you are using CI framework that I don't know well enough to say the same with full surely – anubhava Nov 19 '19 at 08:08
  • i can try if you let me know how to do that on standard, i will update the standard htaccess and i think will work with CI too ! Regards – LIsa Nov 19 '19 at 09:09
  • got the answer $route['(:any)'] = "main/$1"; for routes.php – LIsa Nov 19 '19 at 12:29
  • i am getting err_http2_ping_failed 200, for images and the images does not load when i use the above code in my website as htaccess. any advice sir ?? – LIsa Nov 26 '19 at 17:00
  • Test in Chrome dev tool with **caching disabled** and check in Network tab what are 301/302 redirect URLs you get. – anubhava Nov 26 '19 at 19:12
  • https://stackoverflow.com/questions/59471804/how-to-replace-and-from-url-with-by-htaccess-in-php-ci can you check this please ? – LIsa Dec 24 '19 at 17:28
  • Sorry I am traveling for few days. Will check this once I am back – anubhava Dec 25 '19 at 01:40
  • when ever you are back please check if you can help. thankx – LIsa Jan 02 '20 at 09:15