开发者

CodeIgniter: How to disable "index.php" in URLs completely (disallow it if manually entered in URL)

开发者 https://www.devze.com 2023-01-08 14:22 出处:网络
I\'m using codeigniter and have been able to use url\'s without \"index.php\" in there, but if I manually type in \"index.php\", the urls still work. I\'d like to disable access to \"index.php/control

I'm using codeigniter and have been able to use url's without "index.php" in there, but if I manually type in "index.php", the urls still work. I'd like to disable access to "index.php/controller" type urls in order to avoid duplicate content penalty from Google.

Here is what my current .htaccess file looks like:

Options +FollowSymLinks

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
 Order Deny,Allow
 Deny From All
</Files>

# Protect application and sy开发者_JS百科stem files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]


You can try this by replacing the last 3 lines with this

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

or maybe simply by removing the last line of your .htaccess.

Also you might need to change your /config/config.php to

 $config['index_page'] = “”


The CI manual already gives you an (working) example on how to do it:

You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

Always be sure to check the manual first. It is really good and gives you an easy answer most of the time.

0

精彩评论

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

关注公众号