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.

8
  • Please follow each step stackoverflow.com/questions/19183311/… Commented Nov 16, 2019 at 6:53
  • Be sure your .htaccess file should be in root dir. not in system folder also remove .htaccess file from application folder @Lisa Commented Nov 16, 2019 at 6:55
  • 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 Commented Nov 16, 2019 at 6:58
  • i can remove the index.php from url but i want ot to be auto redirected too. Commented Nov 16, 2019 at 7:46
  • 1
    With the current rules as shown, if you enter domain.com/contact in browser, does it load correct content? Commented Nov 16, 2019 at 14:48

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

4 Comments

its awesome , works but what i want is something from htaccess which redirects auto from main url to preferred url.
how can i do it for all the links in the website automatically ???
$route['(:any)'] = "main/$1";
its working great but not loading this perticular page domain.com/index.php/admin
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]

9 Comments

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 ??
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
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
got the answer $route['(:any)'] = "main/$1"; for routes.php
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 ??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.