如何从Azure上托管的PHP CodeIgniter App中的URL中删除index.php

时间:2017-11-17 17:42:40

标签: php .htaccess codeigniter azure

我试图在Azure上运行PHP codeigniter网站。

但是当我设置

$config['index_page'] = '';

我的config.php它显示错误说"您要查找的资源已被删除,名称已更改,或暂时不可用"

所以,一旦我将其设置为

$config['index_page'] = 'index.php';

我可以加载我的网站。

现在我的网址看起来像这样

http://mysamplesite.azurewebsites.net/helloworld/index.php/Demo

但我希望它是这样的,

http://mysamplesite.azurewebsites.net/helloworld/Demo

但每当我删除index.php部分时,我都会遇到上述错误。

我试图更改我的htaccess文件,但一直收到错误,这是我的htaccess文件,

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

此外,我必须使用site_url()而不是base_url。

那么如何从网址中删除index.php?

4 个答案:

答案 0 :(得分:0)

将它放在.htaccess文件中。它应该工作

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#CI_FOLDER is the Location of your CI files.

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [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>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

答案 1 :(得分:0)

root 目录中创建.htaccess文件 并将此代码放入其中。

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

<Files "index.php">
AcceptPathInfo On
</Files>  

这将有效!!!!

答案 2 :(得分:0)

正如@ourmandave在上面的评论中提到的,在Azure App Service中,您需要一个web.config文件而不是.htaccess,并将以下内容添加到其中:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>

                <rule name="Rewrite to index.php" patternSyntax="Wildcard">
                    <match url="*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>

            </rules>
        </rewrite>
    </system.webServer>
</configuration>

答案 3 :(得分:0)

将以下代码写入.htaccess文件

DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|(.*)\.swf|fonts|images|css|less|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]