【问题标题】:How to setup .htaccess In Codeigniter on Ubuntu 14.04 Lamp Stack Server如何在 Ubuntu 14.04 Lamp Stack 服务器上的 Codeigniter 中设置 .htaccess
【发布时间】:2015-09-01 04:10:25
【问题描述】:

我不知道如何继续,我检查了许多教程一步一步做但没有成功,我开始认为这是我的 apache 上运行不正常的问题。

我不是服务器人,我总是卡在那些东西上,我的网站在我的本地运行良好,但在将其转移到服务器后,问题就开始了。

如果你能帮助我,我将不胜感激。

/etc/apache2/sites-available
mysite.com.conf
   # domain: mysite.com
    # public: /var/www/mysite.com/public_html/

    <VirtualHost *:80>
    #error 403
        <Directory "/var/www/mysite.com/public_html/blog">
            Options FollowSymLinks
            Require all granted
            AllowOverride None
            Require all granted
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
            <IfModule mod_rewrite.c>
              RewriteEngine On
              RewriteBase /blog/
              RewriteRule ^index\.php$ - [L]
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule . /blog/index.php [L]
            </IfModule>
        </Directory>

         <Directory "/var/www/mysite.com/public_html/">
            Options FollowSymLinks
            Require all granted
            AllowOverride All
            Require all granted
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
        </Directory>

        <Directory /var/www/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
        </Directory>

      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html index.php
      DocumentRoot /var/www/mysite.com/public_html
      # Log file locations
      LogLevel warn
      ErrorLog  /var/www/mysite.com/log/error.log
      CustomLog /var/www/mysite.com/log/access.log combined
    </VirtualHost>

/etc/apache2/sites-available

000-default.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    DocumentRoot /var/www/mysite
#for the error 403

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/etc/apache2 apache2.conf

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

应用程序/config/config.php

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

.htaccess 文件在我的根目录

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

【问题讨论】:

    标签: php apache .htaccess codeigniter ubuntu


    【解决方案1】:

    让你的 example.com.conf 像这样简单:

    <VirtualHost *:80>
    ServerName      example.com
    ServerAlias     www.example.com
    DocumentRoot    /var/www/example.com/public_html
    </VirtualHost>
    

    保存并关闭此文件后,确保通过键入以下内容启用您的 VirtualHost:sudo a2ensite example.com.conf

    现在重启apache,sudo service apache2 restart

    这就是 /var/www/example.com/public_html 中的 .htaccess 应该是这样的:

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

    在您的应用程序/config/config.php 中

    $config['base_url'] = 'http://www.example.com/';
    

    尝试这些设置,让我知道它是否适合您。

    【讨论】:

    • 您好,感谢您的帮助,但仍然无法正常工作,请看:我的应用程序在这里: /var/www/mysite.com/public_html/myapp ,我应该将 .htaccess 放在 myapp 的根目录中还是就在public_html中:我都试过了,但还是不行:(
    • 如果你所有的 CodeIgniter 文件和目录都在 myapp 目录下,那么你的 .htaccess 文件应该在 /var/www/mysite.com/public_html/myapp/.htaccess 并且你的 VirtualHost 文件 DocumentRoot 应该指向至:/var/www/mysite.com/public_html/myapp
    • @user964836 试试看。下载 CI 的新副本并将其解压缩到 /var/www/example.com,以便您的应用程序目录应位于: /var/www/example.com/application 。现在按照我上面提到的所有步骤,看看它是否有效。只是为了确认一下,您的重写模块是否已激活?如果没有,请运行以下命令:sudo a2enmod rewrite 并重新启动您的 apache。
    • 我会尝试这样做,是的,我的重写模式已激活,非常感谢我会这样做
    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 2023-03-26
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多