【问题标题】:404 error while trying to access codeigniter url's without index.php尝试在没有 index.php 的情况下访问 codeigniter url 时出现 404 错误
【发布时间】:2014-08-22 06:40:36
【问题描述】:

您好,我最近将托管更改为 AWS。我创建了一个实例并托管了我的网站,并尝试像旧托管一样访问它。但是当我尝试访问 url 中没有 index.php 的内页时,我无法访问它。但如果我使用 index.php 它工作正常。我在 .htaccess 文件中尝试了不同的配置。

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]


<IfModule mod_expires.c>
  ExpiresActive on

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Media: images, video, audio
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
</IfModule>

现在我的配置文件配置如下。

$config['base_url']     = 'http://www.cofounder.in/';

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = 'index.php?';

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'                        Default - auto detects
| 'PATH_INFO'           Uses the PATH_INFO
| 'QUERY_STRING'        Uses the QUERY_STRING
| 'REQUEST_URI'         Uses the REQUEST_URI
| 'ORIG_PATH_INFO'      Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';

【问题讨论】:

  • 我认为基本 url 是问题,因为您使用的是 aws
  • 用那个有什么效果

标签: php .htaccess codeigniter


【解决方案1】:

请确保您的“mod_rewrite”已开启。 可能是这个问题。

改变:

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

$config['index_page'] = '';

.htaccess

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://abc.com/$1 [L,R=301] 
# you want to restrict from url
RewriteCond $1 !^(index\.php|images|mobile|templates|favicon\.ico|robots\.txt)  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
 # can give your server port number not necessary
RewriteCond %{SERVER_PORT} 443

【讨论】:

  • 我认为我的 mod_rewrite 没有启用。我正在尝试启用它但无法启用。我的服务器配置 aws ec2 ubuntu
  • 你能解释一下你在做什么来启用 mod_rewrite 吗?你可以试试这个命令:sudo a2enmod rewrite。然后检查 phpinfo();从 yoursite.com/phpinfo.php,您应该在加载的模块下看到 mod_rewrite。
【解决方案2】:

改变

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

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

然后在您的 htaccess 文件中使用此代码

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
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.
    ErrorDocument 404 /index.php
</IfModule>

有时你可能需要改变

RewriteRule ^(.*)$ index.php/$1 [L]

RewriteRule ^(.*)$ index.php/?$1 [L]

你可能还需要改变

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

将上面提到的其他协议放在 config.php 文件中

【讨论】:

    【解决方案3】:

    这是我的 .htaccess,如果可能的话试试这个

    <IfModule mod_rewrite.c>
        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
        RewriteCond $1 !^(index\.php|images|stylesheets|javascript)
        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> 
    

    在您的配置中将其更改为:

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

    【讨论】:

      【解决方案4】:

      我之前遇到过这个问题,通过更改我的 apache 站点对我有用 AllowOverride None ==> AllowOverride All 在 /etc/apache2/sites-available 中

      在 Ubuntu 中(因为你的是 Ubuntu 系统)

      第一步

      在 apache 中禁用该站点

      sudo a2dissite yoursitename(在我的例子中是默认的)

      第二步

      编辑网站

      sudo vi yoursitename(你可以使用 nano 等其他编辑器代替 vi)

      替换

      AllowOverride None ==> AllowOverride All

      您的虚拟主机最终应如下所示:

      <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/vhosts/example.com
        <Directory /var/www/vhosts/example.com>
          AllowOverride all
        </Directory>
      </VirtualHost>
      

      第 3 步

      启用网站

      sudo a2ensite 默认

      第四步

      重启 Apache

      sudo service apache2 重启

      现在您检查您的应用。它可能会起作用。

      如果您在这方面遇到任何问题,请告诉我。

      类似的discussion here 可能会有所帮助

      【讨论】:

        【解决方案5】:

        做出改变:

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

        $config['index_page'] = '';
        

        .htaccess

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* index.php/$0 [PT,L] 
        
        Options All -Indexes
        

        将此 .htaccess 文件保存在您的根文件夹中

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-22
          • 2015-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多