4
votes

I created a CodeIgniter application and now I'm trying to redirect the urls with index.php to urls without it.

My current .htaccess is:

RewriteEngine On
RewriteBase /

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.plugb.com/$1 [L,R=301]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

The only problem is that the same page is accessible with and without the index.php.

For example:

http://www.plugb.com/index.php/games/adventure-rpg

and

http://www.plugb.com/games/adventure-rpg

Is there a way to redirect the index.php URLs?

Thank you, Gabriel.

5
I did not get it. Are you trying to remove the index.php from the URL?ronaldosantana

5 Answers

3
votes

Add this before the last rule:

RewriteBase /    
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^index.php(/.*)?$ http://%{HTTP_HOST}$1 [R=301]

or to deny access:

RewriteRule ^index.php(/.*)?$ - [R=404]
0
votes

The CodeIgniter wiki documentation describes how to achieve this exact behavior.

This article explains how to take away “index.php” from your CI application URLs. However, it does NOT remove the need for Index.php, which is the CI front controller i.e. even though Index.php will not appear in the URL, it still needs to be present at the top level of your site (above the /system/ directory).

0
votes

Can you try with this? RewriteRule ^(.*)$ index.php/?$1 [L]

Hope so it will works.

0
votes

I came up with an workaround for this:

$clean_url = str_replace('index.php','',current_url());
$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if($clean_url!==$current_url)
{
    redirect($clean_url,'location',301);
}

It's not with .htaccess, but this in your controller can solve the problem. (At least, it worked in PlugB)

0
votes

You dont need to write code to 'redirect' index.php URLS.

Go into application/config/config.php and make the following change

$config['index_page'] = "";