codeigniter index.php删除

时间:2011-04-15 09:10:31

标签: codeigniter mod-rewrite

我有一个多城市应用程序,并为我的应用程序进行了以下设置

这在服务器根目录

之外
framework
   backend
   frontend

CI_2.0.1
   system

现在在服务器根目录中我有2个文件夹来代表2个城市

html
   city1
      city1.php
   city2
      city2.php

我需要添加规则来删除网址中的.php,使其看起来更像

http://example.in/city1/search
http://example.in/city2/search

或完全摆脱index.php文件的规则

2 个答案:

答案 0 :(得分:0)

index.php以及更多.htaccess技巧我使用此代码(CI 2):

# Deny OR Allow Folder Indexes.
# Since we disable access to PHP files you 
# can leave this on without worries. 
# OR better yet, create a .htaccess file in
# the dir you want to allow browsing and
# set it to +Indexes
Options -Indexes

Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>
    # mod_rewrite rules
    RewriteEngine on

    # If a controler 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 forbide all visitors from that site.

    # 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]

</IfModule>

# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

P.S。我认为我没有清楚地了解.php文件的问题。但是,如果你还没有,你会看到native CI routing system它可以用另一种方法解决你的问题)

答案 1 :(得分:0)

让我们从我们的大网址开始:

http://mycustomproject.com/index.php/webpages/view/my-url-title-of-the-post

您通常为Codeigniter项目执行的第一件事是从URL中删除index.php文件。要做到这一点,这很容易:

第一步

将此代码添加到.htaccess文件中,直到项目的根目录。

<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On

# If your website begins from a folder e.g localhost/my_project then 
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /

# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)

# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]

# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)

# No rewriting
RewriteRule ^(.*)$ - [PT,L]

# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>

创建.htaccess文件后,您的项目结构必须如下所示:

website_folder /

---- application /

---- assets /

---- system /

---- user_guide /

---- .htaccess&lt; -------------------------

---- index.php

---- license.txt

第二步

现在转到:application / config / config.php并更改: $ config ['index_page'] ='index.php';

要 $ config ['index_page'] ='';

现在您的网址将如下所示:

http://mycustomproject.com/webpages/view/my-url-title-of-the-post