Codeigniter .htaccess写法

本文介绍如何在CodeIgniter中配置搜索引擎友好的URL,包括启用mod_rewrite模块、设置index页面、创建.htaccess文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CodeIgniter provides the option to use Search Engine friendly URL’s in your application. Search Engine friendly URL’s let’s spiders index your site better and are visually more attractive.

mod_rewrite and the Index page

Before anything else, make sure you have mod_rewrite enabled on your server. Once that’s taken care of, edit your index.php file and set $config['index_page'] to blank.

index.php

[sourcecode]

$config['index_page'] = ”;

[/sourcecode]

Creating the .htaccess file

Go to your root directory, create a file called “.htaccess” and paste the following.

[sourcecode]
# Set the default file for indexes
DirectoryIndex index.php

# mod_rewrite rules
RewriteEngine on

# The RewriteBase of the system (if you are using this system in a sub-folder).
# RewriteBase /SubFolderName/

# This will make the site only accessible without the “www.”
# (which will keep the subdomain-sensive config file happy)
# If you want the site to be accessed WITH the “www.”
# comment-out the following two lines.
# RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
# RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]

# If a controller can’t be found – then issue a 404 error from PHP
# Error messages (via the “error” plugin)
# ErrorDocument 403 /index.php/403/
# ErrorDocument 404 /index.php/404/
# ErrorDocument 500 /index.php/500/

# Deny any people (or bots) from the following sites: (to stop spam comments)
# RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
# RewriteCond %{HTTP_REFERER} porn\.com
# RewriteRule .* – [F]
# Note: if you are having trouble from a certain URL just
# add it above to bock all visitors from that site.

# Or you can also uncomment this if you know the IP:
# Deny from 192.168.1.1

# If the file is NOT the index.php file
RewriteCond %{REQUEST_FILENAME} !index.php

# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)\.php$ index.php/$1

# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

# If mod_rewrite is NOT installed go to index.php

ErrorDocument 404 index.php
[/sourcecode]

转载于:https://my.oschina.net/arunu/blog/144685

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值