Redirect index.php in CodeIgniter
Asked Answered
S

5

4

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.

Sandy answered 18/5, 2010 at 22:37 Comment(1)
I did not get it. Are you trying to remove the index.php from the URL?Holmann
A
3

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]
Algetic answered 18/5, 2010 at 22:53 Comment(5)
I tried this and nothing changed (with and without index.php works).Sandy
yes, id does, but it should forward to the location without index.php. BTW, since you're using RewriteBse /, the rewrite rule should be different. I've updated the answer to reflect thatAlgetic
Using your first solution, I got: --- Moved Permanently The document has moved here. Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request. --- The linkwas redirected, but the content wasn't working. The 'here' word above had a link to the same page. --- I changed [R=301] to [L,R=301] (just trying a solution). It also didn't work. I got a redirection loop. --- I would like to thank you for your help until now. I would appreciate if you can continue to help me.Sandy
Post your apache log and your rewritelog, with rewriteloglevel 9.Algetic
only the relevant part -- the entries that concern the request to index.php And that's the access log, include also the rewrite log. See httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog and httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevelAlgetic
E
0

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).

Earsplitting answered 18/5, 2010 at 22:49 Comment(1)
I also tried this and nothing changed. Both URLs are accessible. I would like to make the URLs with index.php not accessible at all (redirection).Sandy
G
0

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

Hope so it will works.

Ghent answered 19/5, 2010 at 15:17 Comment(0)
S
0

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)

Sandy answered 21/5, 2010 at 19:58 Comment(0)
T
0

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'] = "";  
Ten answered 16/1, 2014 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.