开发者

Redirect index.php in CodeIgniter

开发者 https://www.devze.com 2022-12-31 00:09 出处:网络
I created a CodeIgniter application and now I\'m trying to redirect the urls with index.php to urls without it.

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_FILENAM开发者_运维技巧E} !-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.


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]


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


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

Hope so it will works.


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)


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'] = "";  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号