views:

261

answers:

4

Hello.

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.

A: 

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

Dolph
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).
Gabriel Bianconi
+2  A: 

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]
Artefacto
I tried this and nothing changed (with and without index.php works).
Gabriel Bianconi
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 that
Artefacto
Using your first solution, I got: ---Moved PermanentlyThe 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.
Gabriel Bianconi
Post your apache log and your rewritelog, with rewriteloglevel 9.
Artefacto
Is this what you've asked? http://pastebin.com/EzkVeSiS
Gabriel Bianconi
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 http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog and http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel
Artefacto
A: 

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

Hope so it will works.

shahed
Doesn't work at all...
Gabriel Bianconi
A: 

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)

Gabriel Bianconi