【问题标题】:.htaccess issue | Codeigniter.htaccess 问题 |代码点火器
【发布时间】:2016-10-20 15:04:29
【问题描述】:

我对 Codeigniter 和 MVC 比较陌生。但是,已经成功地“本地”制作了两个应用程序。在探索过程中,我找到了一种从 URL 中删除“Index.php”以及自定义路由的方法。我拥有的 .htaccess 文件在本地就像魅力一样,但是在尝试托管它时;有一个问题 505 内部服务器问题

这是我拥有的第一个 .htaccess 代码(在本地工作):-

<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On

# If your website begins from a folder e.g localhost/my_project then 
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /

# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)

# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]

# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)

# No rewriting
RewriteRule ^(.*)$ - [PT,L]

# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>

当我使用上面的代码时出现错误。而且,该应用程序仅在删除 .htaccess 时才有效。 (然后我不得不使用不方便的长 URL)

经过简短的研究并使用不同的 .htaccess 没有成功,我询问了我的一位成功运行托管 CI 应用程序的朋友。他向我发送了一个文件,该文件可以毫无问题地引导我进入登录页面;但是,不能使用/不使用路由来调用任何函数。即如果我使用自定义路由 URl (www.mySite.com/contact),那么它也会将我带到登录页面,与实际 URL 方案相同 (www.mySite.com/welcome/contact_page)

这里的新代码:-

<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>

  ErrorDocument 404 /index.php
</IfModule>

使用此代码向我显示了登录页面,但我无法从那里进一步导航。当我尝试调用一个函数时,登录页面会重新加载。我认为这是因为最后一个错误处理代码(ErrorDocument 404 /index.php

有谁知道问题可能是什么解决方案??

问题

  • 为什么第一个 .htaccess 代码在托管时不起作用?
  • 第二个可用的 .htaccess 文件可能有什么问题?
  • 你们有更好的 .htaccess 文件吗?如果是的话,你能把它贴在这里吗??

【问题讨论】:

  • 您使用的 CI 版本是什么?如果您使用 CI3,请确保文件名和类名只有第一个字母大写。
  • 我用的是 2。而且,类名的第一个字母大写!!
  • 读取错误信息和status code,http 协议版本可能出错。也检查一下。

标签: .htaccess codeigniter mod-rewrite http-status-code-404


【解决方案1】:

试试这个.htaccessCodeigniter 推荐

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

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

application/config/config.php

$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # Should be empty

并确保控制器名称、模型名称正确。 Bcz Linux 主机区分大小写。

【讨论】:

  • 您好,感谢您的回答。我已经完成了所有这些步骤
  • @RohitNair 那么发生了什么??
  • 没什么!还是一样!!我自己已经完成了所有这些步骤
  • 现在对 htaccess 及其工作原理进行一些研究
  • 现在这样。这是我使用的设置
猜你喜欢
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多