php - Codeigniter URL 重写以删除index.php

标签 php apache .htaccess codeigniter mod-rewrite

首先,我尝试了此处指定的建议:

我正在尝试删除地址栏上臭名昭著的index.php

这是我当前的配置:

In my /etc/apache2/sites-available/my.website.com.conf file i put this:

<Directory /path/to/root/directory/>
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

在我的/etc/apache2/apache2.conf 文件中,我输入了以下内容:

<Directory /path/to/root>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

In my codeigniter root directory (same dir with index.php), i have a .htaccess like this:

<IfModule mod_rewrite>
        RewriteEngine On
        RewriteBase /

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

And finally, my application/config/config.php:

$config['base_url'] = 'http://my.website.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

当我在本地 PC(运行 Ubuntu 14.04)上测试上述配置时,它工作得很好。但是,当我尝试在我的服务器(运行 Ubuntu 14.04)上复制配置时,不知何故它不起作用。

这样做:

  • http://my.website.com/ << 显示默认的 Codeigniter 欢迎页面
  • http://my.website.com/controller_name/ <<显示:

    未找到 在此服务器上找不到请求的 URL/login。 Apache/2.4.7 (Ubuntu) 服务器位于 my.website.com 端口 80

  • http://my.website.com/index.php/controller_name << 显示页面,但是无法正确获取静态文件(即 JS/CSS/图像)的 URL,因为打印此内容:

      <link rel="stylesheet" href="assets/styles/material-design-icons.css" type="text/css" />
      <link rel="stylesheet" href="assets/styles/font.css" type="text/css" />
      <link rel="stylesheet" href="assets/styles/app.css" type="text/css" />
    

当从源代码检查器中看到时,会附加index.php

我在这里错过了什么吗?有人能指出我正确的方向吗?

谢谢。

编辑 #1 下面是我的文件夹结构

Dir Structure

编辑 #2 我可以确认 mod_rewrite 已启用

PHP Info

最佳答案

好的,您提到了很多与您的问题相关的堆栈链接。

更改此设置

$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

并将其添加到.htaccess

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

Note: Some time in localhost index.php will not removed.

包含您的 css/js/images

CSS:<link rel="stylesheet" href="<?php echo base_url()?>assets/styles/material-design-icons.css" type="text/css" />
JS : <script type="text/javascript" src="<?php echo base_url(); ?>assets/js/in/easing.js"></script>
Img: <img src="<?php echo base_url()?>assets/image/logo.jpg" alt=""/>

所以文件结构是

- application
- assets
  - style
     - material-design-icons.css
     - font.css
     - app.css
  - js
     - easing.js
  - image
     - logo.jpg
- index.php
- .htaccess(Post in my code)

编辑01

config/routes.php

$route['default_controller'] = "";//give default controller name
$route['404_override'] = '';

关于php - Codeigniter URL 重写以删除index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853184/

相关文章:

apache - mod_rewrite : Rewrite URL to point to cgi script, 但保持重写隐藏

javascript - PHP Javascript - 数组多维错误

php - 如何通过 Laravel Blade 获取页面名称

apache - htaccess mod_rewrite 主机名

python - 可以在特定端口上运行带有 mod_wsgi 的 Flask 应用程序吗?

javascript - 使用 jquery 向非 native 服务器发送授权 header 时出错

apache - 防止直接文件访问

PHP:回显结果编号

php - 使用 PHP 创建 CRUD 的最简单方法

macos - 我的系统上运行了 2 个版本的 Apache