Apache中的mod_rewrite问题(使用WAMP)

时间:2011-08-16 19:41:16

标签: apache .htaccess mod-rewrite wamp

理念:我想说的是,在域名后面以.html结尾的任何内容都被index.php视为get变量

示例:www.test.ro/1/2/3.html实际上应该是www.test.ro/index.php?var=1/2/3.html

www.test.ro设置为开发的虚拟主机,AllowOverride的值为All。

.htaccess文件似乎已被处理,但并非总是如此。如果我写了一个像'BizzareRule'这样的不公认的规则,服务器可以在不返回代码500错误的情况下工作。 如果我在<IfModule mod_rewrite.c></IfModule>之间放置了正确的规则,我会得到500错误,即使我有其他vhost使用相同的条件并且工作正常。

以下是我的vhost的内容:

<VirtualHost *:80>
    ServerAdmin eu@localhost.com
    ServerName  www.test.ro
    ServerAlias test.ro

    DocumentRoot D:/Projects/grabsite/test.ro
    Options Indexes FollowSymLinks MultiViews

    DirectoryIndex index.php index.html

    LogLevel warn
    ErrorLog D:/Projects/grabsite/test.ro/error.log
    CustomLog D:/Projects/grabsite/test.ro/access.log combined

    <Directory "D:/Projects/grabsite/test.ro">
        Options -Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

测试.htaccess的内容:

RewriteEngine On
RewriteRule ^/?([^/]*)\.html$ /index.php?seo=$1 [L]

2 个答案:

答案 0 :(得分:1)

似乎你进入无限循环。首先尝试简化规则: RewriteRule ^(.*)\.html$ /index.php?seo=$1 [L]

或者只是 RewriteRule ^index.html$ /index.php?seo=$1 [L]

接下来,最好有RewriteCond -f和RewriteCond -d,以便在请求现有文件/文件夹时不执行规则。在某些情况下,这可以防止规则中的无限循环

答案 1 :(得分:0)

问题在于正则表达式。你说:“.htaccess文件似乎已被处理,但不是所有时间”

如果您检查模式^/?([^/]*)\.html$,您会发现这只适用于仅在根文件夹中的文件(例如/index.html)。

解决方案:将模式更改为^(.+)\.html$,一切正常 - 现在它将匹配以 .html结尾的 ANY 网址(例如/index.html以及/hello/pink-kitten.html等)。