簡體   English   中英

使用您自己的本地域時,使用.htaccess文件在CodeIgniter中刪除index.php

[英]Removing index.php in CodeIgniter using .htaccess file when using your own local domain

我看過其他有關同一問題的文章。 但是,沒有答案能解決我的問題。

我沒有使用xampp或wamp,但是分別設置了php mysql apache。 另外,我正在使用自己的本地域,例如mytestsite.com。

我的.htaccess文件如下所示:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /TestCI/

    # Canonicalize Codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(HomeController(/index)?|index(\.php|html?)?)/?$ / [R=301,L]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [R=301,L]

    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|TestCI) [NC]
    RewriteRule ^(.*)$ http://www.mytestsite.com/$1 [R=301,L]

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

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

我還對config.php進行了以下更改

$config['base_url'] = 'http://mytestsite.com/TestCI/';    
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO'; //AUTO

我還對apache2.conf進行了以下更改

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

但是當我嘗試訪問

http://mytestsite.com/TestCI/HomeController/

其顯示404錯誤,但在使用時可以正常工作

http://mytestsite.com/TestCI/index.php/HomeController/

我調查了一些已經解決的類似問題,但是沒有一個答案對我有用。 還有什么需要做的事情才能從網址中刪除index.php 請回復。 高度贊賞。

在您的.htaccess文件中替換以下代碼行

RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

只需將此代碼放在您的.htaccess中:

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

可能是未使用htaccess。

在文件中放入一個語法錯誤。 (將類似hdtbf的內容添加到文件中)

如果那沒有給出500錯誤,則需要在主Apache配置中啟用allow override all

http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride

謝謝您的幫助...問題已解決。

我在apache2.conf文件中進行了以下更改

<Directory /Absolute path to .htaccess folder

        AllowOverride All
        Require all granted
</Directory>

我也刪除了.htaccess文件中的RewriteBase參數,這是實際的pbm所在的位置。

我當前的.htaccess文件看起來像

<IfModule mod_rewrite.c> 
    RewriteEngine On


    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php 
</IfModule>

我還對config.php進行了更改,其中我將$ config ['base_url']更改為codeigniter根路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM