【问题标题】:issue with Codeigniter .htaccess rulesCodeigniter .htaccess 规则的问题
【发布时间】:2018-11-16 01:01:58
【问题描述】:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

在我的.htaccess 文件中得到了这个(如https://www.codeigniter.com/user_guide/general/urls.html 中所述),虚拟主机的apache 设置如下所示:

<VirtualHost *:80>
    ServerName fitness.local
    ServerAdmin webmaster@localhost
    ServerAlias www.fitness.local
    DocumentRoot /var/www/blast

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

然而,如果我想去http://fitness.local/events 它不工作,但http://fitness.local/index.php/events 工作。我已经为 apache 启用了 mod rewrite,所以这也包括在内。

我该如何解决这个问题?

【问题讨论】:

  • 您是否删除了配置中的“index.php”?
  • @DamienPirsy 是的,我做到了,但这并没有解决问题。
  • 启用mod_rewrite后你重启服务器了吗?
  • @MuhammadRashid 是的,我这样做了,但也没有用。
  • 尝试在您的 config.php 中设置 $config['uri_protocol'] = 'AUTO';

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

在你的 .htacess 文件中试试这个然后运行

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

【讨论】:

  • 感谢您的想法,但这并不能真正解决问题。
【解决方案2】:

这样可以正常工作:

<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 /dude_code/

    # 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 ^(main\.php|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 文件放在其他地方或 smthn? :D
  • RewriteBase /dude_code/ 将 dude_code 更改为 htdocs 中代码的完整路径
  • 你的意思是例如...?
  • 不知道您能否给我看一个带有路径的示例 - 即/var/www/blast 或者我应该给它什么样的路径?
【解决方案3】:

将您的 htaccess 更改为

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

打开你的 httpd conf 文件检查是否启用了 mod-rewrite

LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride All

然后重启你的 Httpd。

这在我的情况下总是有效的。

谢谢

【讨论】:

    【解决方案4】:

    使用此 .htaccess 访问您的项目。

    (应该放在应用程序文件夹之外

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-20
      • 2014-07-17
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多