RewriteRule 创建 500 内部服务器错误 [英] RewriteRule creating 500 Internal Server Error

查看:23
本文介绍了RewriteRule 创建 500 内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 .htaccess 文件中有以下内容:

I have the following in my .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?id=$1

我想要实现的是:

当访问 URL www.mydomain.com/directory/10 时,页面 www.mydomain.com/directory/?id=10 显示在浏览器而不改变 URL 的外观.

When the URL www.mydomain.com/directory/10 is visited, the page www.mydomain.com/directory/?id=10 is displayed on the browser without altering the appearance of the URL.

上面的代码虽然创建了一个 500 内部服务器错误.

The above code creates a 500 Internal server error though.

有人知道我哪里出错了吗?

Does anyone know where I'm going wrong?

推荐答案

你的代码肯定会产生 500 个内部服务器错误,因为它会导致无限循环.原因是您匹配的 URI 模式是:^directory/(.*)$

Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^directory/(.*)$

在重写之前和之后匹配您的 URL.一旦达到最大允许的内部重写限制,Apache 会抛出 500 个内部服务器错误并退出.

Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.

将您的代码更改为:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^directory/(.*)$ /directory/index.php?id=$1 [L,QSA,NC]

上面的代码有一个额外的 RewriteCond %{REQUEST_FILENAME} !-f,它确保在第一次之后禁止 RewriteRule 的后续执行,因为 /directory/index.php将是一个有效的文件.

Above code has an extra RewriteCond %{REQUEST_FILENAME} !-f that will make sure to disallow subsequent execution of RewriteRule after first time since /directory/index.php will be a valid file.

这篇关于RewriteRule 创建 500 内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆