.htaccess 在服务器上不工作

.htaccess not working on server

我已经将网站托管为子文件夹,例如 "http://www.sitename.com/projectfoldername/"。 这里它只显示主页,当点击任何菜单时它显示 404 错误,所以我通过在控制器名称前写 index.php 来更改 url 现在 url 已经从这个改变了: "http://www.sitename.com/projectfoldername/controller-name/function-name" 对此:"http://www.sitename.com/projectfoldername/index.php/controller-name/function-name" 然后它显示页面但仅在本地工作..

我的 htaccess 文件

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

config.php

$config['base_url'] = 'http://www.sitename.com/projectfoldername/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

在您的 .htaccess 文件中使用 RewriteBase /projectfoldername,如下所示:

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

尝试在 .htaccess 中添加以下内容,它可能对您有所帮助。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

@antroo ghosh ,首先您必须知道正在使用哪个 Web 服务器(Apache 或 IIS)。

如果您使用的是 windows(IIS) 服务器,则不能使用 .htaccess,在 IIS 中,您必须使用 web.config 而不是 .htaccess。

更多详情https://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/translate-htaccess-content-to-iis-webconfig

希望对您有所帮助!

如果您使用 Apache Web 服务器,请激活您的 mod_rewrite

在 Ubuntu 上:

sudo a2enmod rewrite

在 Window:

打开您的 httpd.conf 并找到行

#LoadModule rewrite_module modules/mod_rewrite.so and remove the hash ‘#’