Codeigniter Htaccess and Url Rewrite Issues

CodeIgniter htaccess and URL rewrite issues

There are 3 steps to remove index.php.

  1. Make below changes in application/config.php file

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
  2. Make .htaccess file in your root directory using below code

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
  3. Enable the rewrite engine (if not already enabled)

    i. First, initiate it with the following command:

    a2enmod rewrite

    ii. Edit the file /etc/apache2/sites-enabled/000-default

    Change all AllowOverride None to AllowOverride All.

    Note: In latest version you need to change in /etc/apache2/apache2.conf file

    iii. Restart your server with the following command:

    sudo /etc/init.d/apache2 restart

Codeigniter Htaccess and URL Redirect issues

This simple .htaccess will remove your index.php

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

Change your check function like this

 public function check(){
$this->load->model('check');
$logcheck = $this->check->check($this->input->post('username'),$this->input->post('password'));

if($logcheck == "admin"){
//may be you need to set login credential into session
redirect('Phonebook/home/');
//$this->load->view('home');
}else{
$this->load->view('login');
}
}

and your home function will be like this

public function home(){
//remember you need to check login validation from session
$this->load->view('home');
}

to use redirect function remember you have url helper loaded.

May be this help you

Codeigniter Url rewriting not working it is redirecting to not found page

Use this .htaccess

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

and in config.php

$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = '';

Codeigniter URL rewriting .htaccess is not working on CentOS

You need to also specify the locations that can use it. For example, in /etc/httpd/conf/httpd.conf you should see something like:

<Directory "/var/www/html">

...lots of text...

</Directory>

Make sure is has:

<Directory "/var/www/html">

AllowOverride All

</Directory>

Codeigniter .htaccess redirect url issue

After RewriteEngine On in new line add this rule:

RewriteRule ^pages/product_popup/1$ /product-syrup [R=301,L]

CodeIgniter .htaccess index.php rewrite not working on user directory

Well, I found a solution in codeigniter forums, first I need to add RewriteBase line as follows:

RewriteBase /~userdir/myproject/

Then, remove the slash before index.php on last line.

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Someone said that

RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favico​​n\.ico)

is redundant, the following two lines of code cover it,

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

and all other real files/directories. They basically say "if the request is not for a REAL file, and the request is not for a REAL directory, pass the request to index.php. Presumably everything from the above line is a real file or directory, so it's not needed at all.

So, my final .htaccess is like this:

RewriteEngine on
RewriteBase /~userdir/myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

It works well locally with http://localhost/~userdir/myproject also on production with http://example.com/~userdir/myproject

Why is .htaccess url rewrite not working codeigniter mamp?

I replaced my own index with the default CI index (that is, i returned it to how it was out of the box).
I then routed domain/index.php/pages/view/page.php to domain/index.php/page.php

i then ran Nishant's .htaccess code and although it didn't appear to work (a static non-style version of the site displayed at the usual url but not at the url without the index.php) it may have helped get me to a solution.
i also changed RewriteEngine On to RewriteEngine on though im pretty sure i had tried that before.
After these steps i then flicked back over to the code that sbaaaang suggested (which was somewhat a version id already unsuccessfully tried previously) and this time it worked.


For anyone else who might run into .htaccess trouble i would suggest leaving things as they come out of the box and going through the tutes (instead of jumping into it like i did).

My .htaccess ended up like this

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


<Files "index.php">
AcceptPathInfo On
</Files>
</IfModule>

<IfModule !mod_rewrite.c>

ErrorDocument 404 /index.php
</IfModule>

and config vars like:

$config['base_url'] = '';
$config['index_page'] = '';

.htaccess and codeigniter not working

What do you mean when you say "it doesn't work"? Do you get an error? A white screen? A 500 error?

Did you make sure your httpd.conf file has the AllowOverride setting set to All? If not, it may not be using your .htaccess file, meaning the rewrite rules won't be used.



Related Topics



Leave a reply



Submit