htaccess RewriteRule不能正常工作


htaccess RewriteRule doesn't work properly

我在我的.htaccess文件中有这个代码:

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

得到的是404虽然我可以用不漂亮的url访问它,比如index.php?link=somepage

我的完整网址是http://www.website.com/subfolder/index.php?link=somepage

我在Ubuntu 16上安装了Apache 2.4.18并且加载了mod_rewrite模块。

也当我尝试用这个:

RewriteRule ^ index.php [L]

然后我可以用http://www.website.com/subfolder/?link=somepage
访问它所以当删除index.php

时,它是有效的

是否有可能在服务器上设置一些额外的配置?

这是一个服务器问题,包括mime类型和多视图。

对于stackoverflow来说这并不是真正的问题,但是如果有人遇到同样的问题,这里有解决这个问题的资源:

  • http://www.bennadel.com/blog/2218-negotiation-discovered-file-s-matching-request-none-could-be-negotiated.htm -从虚拟主机配置中删除MultiViews

  • https://serverfault.com/questions/372733/apache-file-negotiation-failed -添加缺失的mime类型

尝试:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /subfolder/index.php?link=$1 [L]

试试这个:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?link=$1

首先你应该检查你的apache配置

sudo vi /etc/apache2/apache2.conf

搜索

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

AllowOverride None更改为AllowOverride All

通过运行cmd

启用htaccess模块
sudo a2enmod rewrite

然后重新启动服务器

sudo service apache2 restart
并在项目文件夹 中使用这个。htaccess文件
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

希望它对你有用…:)