How do I write a .htaccess file to make CodeIgniters URL routing work?

后端 未结 9 1113
死守一世寂寞
死守一世寂寞 2020-12-01 01:13

I\'m running a LAMP environment with CodeIgniter. I want to be able to use its URL pattern, like, http://localhost/controller/function/ID, but by default it has

相关标签:
9条回答
  • 2020-12-01 01:41

    Tried the answer here but that didn't work for me. So i tried the following and it did...

    create the .htaccess in the root directory where the index.php along with license.txt is available. enter the following code in the file and store it:

    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  
    

    done! try it and it will surely work. Hope it helps

    0 讨论(0)
  • 2020-12-01 01:43

    I had the same issues with CodeIgnitor links images,js,css. It all works now thanks to you guys! I have followed the below steps. consolidated all other comments into my scenario to work. Hope this helps somebody.

    1. changed httpd.conf

      <Directory --your root dir-- />
          AllowOverride All #was "none", but this was all commented out
          Order Deny,Allow
          Deny from all
      </Directory>
      
    2. created an empty .htaccess file in the htdocs\yoursite(site root) directory and pasted the following.

      DirectoryIndex index.php
      RewriteEngine on
      RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
      
    0 讨论(0)
  • 2020-12-01 01:55

    This particular .htaccess file looks like it needs to go in your site's root (meaning, the currently executing site.)

    Look into the documentation for RewriteCond to learn about what other kinds of conditions you can specify. Basically a RewriteRule will only be applied if all of its preceding RewriteConds are matched. You could try adding these rules to only redirect if the requested URL does not exist:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    mod_rewrite is very powerful, but it can be a real pain if it's not doing what you think it should. I don't know of any way to debug its directives, either.

    As far as what else you can do with a .htaccess file, the Apache docs for htaccess files are always a good place to start. The most common use is for per-directory authentication. Most server configuration directives are supported in a .htaccess file (provided that the server's AllowOverride says so), but some have different syntax.

    0 讨论(0)
提交回复
热议问题