Apache 中的 mod_rewrite 问题(使用 WAMP)

发布于 2024-11-30 01:39:52 字数 1332 浏览 0 评论 0原文

想法:我想让域名后面以 .html 结尾的任何内容都被 index.php 视为 get 变量

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

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

.htaccess 文件似乎已被处理,但并非始终如此。如果我编写了像“BizzareRule”这样的不可识别的规则,服务器将正常工作而不会返回代码 500 错误。 如果我在 之间放置正确的规则,我会收到 500 错误,即使我有其他虚拟主机使用相同的条件并且工作正常。

以下是我的虚拟主机的内容:

<VirtualHost *:80>
    ServerAdmin [email protected]
    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]

The Idea: I want to make it that anything that comes after the domain name and ends with .html is treated as a get variable by index.php

Example: www.test.ro/1/2/3.html should actually be www.test.ro/index.php?var=1/2/3.html.

www.test.ro is setup as a virtual host for development, and AllowOverride has value All.

The .htaccess file seems to be processed, but not all the time. If I write a non-recognized rule like 'BizzareRule', the server works without returning a code 500 error.
If I put correct rules between <IfModule mod_rewrite.c></IfModule>, I get an 500 error, even though I have other vhosts using the same conditions and working perfectly.

Here are the contents of my vhost:

<VirtualHost *:80>
    ServerAdmin [email protected]
    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>

And test content for .htaccess:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人如初 2024-12-07 01:39:52

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

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

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

It seems that you enters in infinite loop. Try first to simplify the rule:
RewriteRule ^(.*)\.html$ /index.php?seo=$1 [L]

Or just
RewriteRule ^index.html$ /index.php?seo=$1 [L]

Next, it's best to have RewriteCond -f and RewriteCond -d in order not to execute the rule if you are requesting existing file/folder. In some situations, this can prevent an infinite loop in the rule

浅暮の光 2024-12-07 01:39:52

问题出在正则表达式模式中。您说:“.htaccess 文件似乎已被处理,但并非一直被处理”。

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

解决方案:将模式更改为^(.+)\.html$,一切都会好起来的——现在它将匹配以ANY结尾的URL与 .html (例如 /index.html 以及 /hello/pink-kitten.html 等)。

The problem is in regex pattern. You said: "The .htaccess file seems to be processed, but not all the time".

If you check your pattern^/?([^/]*)\.html$ you will see that this will work for files in root folder ONLY (e.g. /index.html).

Solution: change pattern to ^(.+)\.html$ and all will be fine -- now it will match ANY URL that ends with .html (e.g. /index.html as well as /hello/pink-kitten.html etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文