简体   繁体   中英

routing requests with htaccess and codeigniter

I have a codeigniter app with one controller (main.php)

Currently, I have the htaccess file set to remove the index.php and the main.php

So instead of www.domain.com/index.php/main/function_name, it's just www.domain.com/function_name

My htaccess files looks like this:

 <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /


RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]


RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/main/$1 [L]

ErrorDocument 404 /index.php

I now need to add a large piece to the site. It's currently built in a seperate codeigniter app, and I need to move it over. The new part of the site is in controllers/manage_emails/contacts.php...

My question is, how do I change the htaccess file to remove the main.php from the URL for most of the time, but then if you put in www.domain.com/manage_emails/contollername it would go to the proper controller.

Thanks!

From what I understand there are 2 separate problems you have:

1.remove the index.php file from Codeigniter's default

2.redirect any call from 'www.domain.com/main/method' to 'www.domain.com/method'

For the first problem, I always use Elliot Haughin's htaccess file for CI: CodeIgniter and mod_rewrite with multiple applications

For your second problem, you need to change config/routes.php in CI, and add this line:

$route['(:any)'] = "main/$1"; 

..and you can also check this post: Codeigniter, bypassing the main or default controller

Try to separate these 2 problems, and like it has been said, stick with CI for routing and just use htaccess for removing index.php from URL.

Your application would be a lot easier to manage if you remove any URI routing from the .htaccess file, and let your CodeIgniter's routes do that for you.

Your .htaccess should have just the standard "get rid of index.php" code (the last part, without the /main in it). Then your application's routes can define when/where the rest of the URLs will go.

Slightly unrelated FYI, if you are using a more recent version of CI such as 2.1.x, you don't need the system and application folder-specific rules in your .htaccess.

In addtion to @despina answer i want to add something:

You have to set $route['(:any)'] = "main/$1"; at the last route because CI handle the routes in the order in which they appear on your routes.php file, so if you put $route['(:any)'] at the top, it will handle anything.

so your route.php file will be something like that:

$route['controllers/manage_emails/something.php'] = 'controllers/manage_emails/something.php'
$route['(:any)'] = "main/$1"; 

First of all your 2 rules are not correct and most likely not working. Here is the fixed version of your .htaccess with addition of one new rule that have asked for:

RewriteEngine On
RewriteBase /

RewriteRule ^(?:application|system)(/.*|)$ /index.php?/$1 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/manage_emails/contollername [NC]
RewriteRule ^(.*)$ index.php?/main/$1 [L]

ErrorDocument 404 /index.php

//config/routes.php
    $route['404_override'] = 'main/route';
//controllers/main.php
class Main {
...........
   function route() {
      $methodName = $this->uri->segment(1);
      if(method_exists($methodName, $this)) {
       $this->{$methodName}();
      } else {
        show_404();
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM