无法获取mod_rewrite IfModule在.htaccess中工作

时间:2013-01-25 03:36:50

标签: php c .htaccess mod-rewrite

我正在尝试使用我的.htaccess文件将所有网址转发到包含网址参数的单个网页,以便我可以通过这种方式处理网页检索。我想要发生的是:假设用户在http://mysite.com/users/dan中键入,它应转发到页面http://mysite.com/index.php?url=/users/dan

同样,如果用户访问了网址http://mysite.com/random-link,则应转发至http://mysite.com/index.php?url=random-link

以下是我在.htaccess文件中尝试过的代码,但它只是向我抛出500错误:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond % (REQUEST_FILENAME) !-f
RewriteRule (.*)$ index.php?url=$1 <QSA,L>
</IfModule>

我更新了我的代码并且仍然有500错误 我改变了&lt;和&gt;到[和]我删除了RewriteCond中%之后的空格,但它仍然会抛出错误。

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

对于.htaccess,我是新手,所以任何帮助都会非常感激,因为我不知道是什么导致服务器超时。

1 个答案:

答案 0 :(得分:5)

一些问题

  1. 重写标记放在方括号中,例如

    [QSA,L]
    
  2. 您的RewriteCondition语法看起来不正确。尝试

    RewriteCond %{REQUEST_FILENAME} !-f
    
  3. 为了安全起见,将表达式锚定到字符串的开头

    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    
  4. 最后,RewriteEngine和后续修饰符需要FileInfo override。确保文档根目录的服务器配置或虚拟主机<Directory>部分具有

    AllowOverride FileInfo
    

    更新

    这是MVC项目的典型重写方案。这将忽略真实文件,目录和符号链接

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]