【问题标题】:Removing index.php from CodeIgniter url redirects to localhost从 CodeIgniter url 中删除 index.php 重定向到 localhost
【发布时间】:2015-03-19 16:58:41
【问题描述】:

我正在使用 WampServer 进行开发,并尝试从 CodeIgniter 网址中删除 index.php。正如我在 ellislab 网站上看到的那样,将这些行添加到 .htaccess 文件中: https://www.codeigniter.com/user_guide/general/urls.html

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

这是我网站的根目录: http://localhost/job/

但是现在,当我想浏览像 http://localhost/job/seeker 这样的网址时,我看到了 http://localhost/ 上的 Wampserver 配置页面。

我能做什么?

谢谢

【问题讨论】:

标签: php .htaccess codeigniter url-rewriting


【解决方案1】:

以下代码取自http://www.farinspace.com/codeigniter-htaccess-file/

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

### Canonicalize codeigniter URLs

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

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

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

# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

###

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

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

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

确保 .htaccess 文件的名称正确且与 index.php 文件位于同一目录中,并且在服务器堆栈上的 Apache 中启用了 mod_rewrite。

同时确保您再次正确设置您的 Code Igniter 配置,根据相同的链接,编辑您的配置文件。

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

$config['index_page'] = "";

另外,请使用搜索。这个问题在很多地方都会弹出很多答案。

【讨论】:

  • 太有趣了。我无法让它运行,但为以后使用添加了书签。谢谢。
【解决方案2】:
RewriteEngine on
RewriteBase /job
RewriteCond $1 !^(index\.php|images|table-images|robots\.txt|styles|js|uploads)
RewriteRule ^(.*)$ index.php/$1 [L]

并在 application/config/config.php 中设置

$config['base_url'] = 'http://127.0.0.1:8000/job/';
$config['index_page'] = 'index.php';

【讨论】:

  • 在不更改配置的情况下完成了这项工作。谢谢
  • 多哈。我应该注意到子目录。在这点上,我建议对项目使用虚拟主机而不是本地主机。
【解决方案3】:

它发生在 wamp 服务器上试试这个

RewriteEngine On
RewriteBase /name_of_your_project
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

【讨论】:

  • 谢谢。有用。但是我为什么要添加 REQUEST_FILENAME 行呢?
  • 嗯,对于某些托管来说,需要 wamp 服务器几个版本
【解决方案4】:

试试这个:

     RewriteEngine On
     RewriteBase /projects/hc/homecare/trunk/homecare 
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
     RewriteRule ^(.*)$ /projects/hc/homecare/trunk/homecare/index.php?/$1 [L]

【讨论】:

    【解决方案5】:
       **
    

    删除 index.php 并将 anyurl.com 重定向到 codeigniter 中的 www.anyurl.com

    **

       RewriteEngine on
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule .* index.php/$1 [PT,L]
       RewriteCond %{HTTP_HOST} !^www\.
       RewriteCond %{HTTPS}s on(s)|offs()
       RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R] 
    

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 2020-05-26
      • 1970-01-01
      • 2016-05-14
      • 2014-10-28
      • 2012-06-15
      • 2012-09-21
      相关资源
      最近更新 更多