Apache 重写没有 mod_rewrite?

发布于 2024-12-27 02:10:37 字数 3585 浏览 2 评论 0原文

在没有启用或配置 mod_rewrite 的情况下,Apache 如何重写 URL?

我正在从我的开发服务器重新设计网站。我在网站部分使用目录结构,但我正在考虑切换到 URL 重写并将相关文件放在网站的根目录中。

例如,about 页面有一个名为 about 的子目录,其中包含一个 index.php,当请求为 http:// /www.example.com/about。我计划更改此设置,以便即使请求不包含 php 扩展名,也可以提供站点根目录中的 about.php 服务。

在我的开发服务器上,问题是 URL 已经被重写,即使我在路径层次结构中的任何位置都没有 .htaccess 文件。

Apache 配置是 Ubuntu 的默认配置,我怀疑这是 Ubuntu 特有的配置,但我找不到原因。

以下是我的一些观察结果:

  • /etc/apache2/httpd.conf 是空的,但配置位于 /etc/apache2/apache2.conf 中,而 /etc/apache2/apache2.conf 又包含一些配置文件其他目录。
  • 该站点层次结构中的任何位置都没有 .htaccess 文件。我已经检查了从 /var/www 开始的每个目录,
  • 除了 /etc/apache2/mods-available 中的 rewrite.load 文件之外,/etc/apache2 下没有 .conf 或 .load 文件有任何重写参考。
  • 我已设置 LogWarning 指令进行调试。当我访问不带 .php 扩展名的页面时,错误日志显示带有扩展名的 URL,访问日志显示不带扩展名的 URL 和 HTTP 200 状态。页面服务正常。
  • 如果我添加具有适当配置的 .htaccess 文件,则会收到 .htaccess: Invalid command 'RewriteEngine' 错误。 ...默认情况下甚至没有加载 mod_rewrite 。
  • 如果我通过链接到 /etc/apache2/mods-enabled 中的加载文件来启用 mod_rewrite ,它会按预期工作,也就是说,与我没有启用它时完全相同。

access.log 显示:

127.0.0.1 - - [16/Jan/2012:05:36:35 +0800] "GET /dev/ghodmode.com/h5bp/ HTTP/1.1" 200 3602 "http://alienware/dev/ghodmode.com/h5bp/experiments" "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:8.0) Gecko/20100101 Firefox/8.0"

而 error.log 显示:

[Mon Jan 16 05:23:23 2012] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 5347 to 1876 : URL /dev/ghodmode.com/h5bp/experiments.php, referer: http://alienware/dev/ghodmode.com/h5bp/

如果我添加 .htaccess 文件:

[Mon Jan 16 05:34:33 2012] [alert] [client 127.0.0.1] /var/www/dev/ghodmode.com/h5bp/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration, referer: http://alienware/dev/ghodmode.com/h5bp/experiments

更新

感谢提供的答案,我一直在阅读并学到了一些东西。其要点是我正在使用 Apache2 的 Ubuntu 安装,并且我没有更改任何默认配置设置。因此,没有设置任何建议的原因。

当然,我可能误解了一些文档。这是我第一次阅读其中一些选项。

多视图
Multiviews 提供了这种情况的最佳解释,但这需要输入地图或为路径设置的 MultiViews 选项。我只有一种“var”类型映射(/etc/apache2/mods-enabled/mime.conf),并且 MultiViews 选项仅为 /usr/share/apache2/icons 设置。所以,这不可能是这样。

重定向和重定向匹配
重定向RedirectMatch 将为这种行为提供一个很好的解释,但它需要每个重定向都有一个指令。我没有设置任何这些,并且我输入的任何路径都会提供相关的 php 文件(如果存在)。另外,如果这是原因,我认为如果是重定向,access.log 将生成 3xx 状态而不是 200。

别名和AliasMatch
别名和别名AliasMatch,如重定向和别名匹配RedirectMatch,需要为每个资源设置一个指令。我只为 /icons/ 设置了别名。

后备资源
FallbackResource 标识在请求的资源不存在时要提供的单个资源t 呈现。就我而言,当请求的资源不存在时,它会通过在请求末尾附加“.php”来提供找到的资源。我还仔细检查过,没有设置 FallbackResource 指令。

下面是我用来确认某个特定选项未在任何地方设置的命令示例:

find /etc/apache2 -name "*.conf" -exec grep -li "FallbackResource" {} \;

更新 #2:

我搜索 MultiViews 选项时出现了漏洞。我只是在寻找具有 .conf 或 .load 扩展名的文件,但某些配置文件根本没有扩展名。 MultiViews 选项由默认站点配置文件 /etc/apache2/sites-enabled/000-default 设置。

感谢@zneak 首先提供了答案,尽管他是在评论中提供的。感谢@regilero,他通过使用与我习惯的语法不同的 grep 语法间接向我展示了我所缺少的内容。

How can Apache be rewriting URLs without mod_rewrite enabled or configured?

I'm working on a site redesign from my development server. I was using a directory structure for site sections, but I am considering switching to URL rewriting and putting related files in the site's root.

For example, the about page has a subdirectory named about that contains an index.php which is served when the request is http://www.example.com/about. I plan to change this so that about.php from the site's root directory is served even if the request doesn't include the php extension.

On my development server, the problem is that the URLs are already being rewritten even though I don't have an .htaccess file anywhere along the path hierarchy.

The Apache configuration is the stock Ubuntu configuration and I suspect that it's something unique to Ubuntu, but I can't find the cause.

Here are some observations I've made:

  • /etc/apache2/httpd.conf is empty, but the configuration is in /etc/apache2/apache2.conf which in turn Includes config files from a few other directories.
  • There were no .htaccess files anywhere in the hierarchy of this site. I've checked every directory starting at /var/www
  • No .conf or .load files under /etc/apache2 have any reference to rewrite other than the rewrite.load file in /etc/apache2/mods-available.
  • I've set the LogWarning directive to debug. When I access a page without the .php extension, the error log shows the URL with the extension and the access log shows it without the extension and a HTTP 200 status. The page is served properly.
  • If I do add a .htaccess file with the appropriate configuration, I get an .htaccess: Invalid command 'RewriteEngine' error. ... mod_rewrite wasn't even loaded by default.
  • If I enable mod_rewrite by linking to the load file in /etc/apache2/mods-enabled it works as expected, which is to say exactly the same as if I didn't enable it.

access.log shows:

127.0.0.1 - - [16/Jan/2012:05:36:35 +0800] "GET /dev/ghodmode.com/h5bp/ HTTP/1.1" 200 3602 "http://alienware/dev/ghodmode.com/h5bp/experiments" "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:8.0) Gecko/20100101 Firefox/8.0"

while error.log shows:

[Mon Jan 16 05:23:23 2012] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 5347 to 1876 : URL /dev/ghodmode.com/h5bp/experiments.php, referer: http://alienware/dev/ghodmode.com/h5bp/

If I add an .htaccess file:

[Mon Jan 16 05:34:33 2012] [alert] [client 127.0.0.1] /var/www/dev/ghodmode.com/h5bp/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration, referer: http://alienware/dev/ghodmode.com/h5bp/experiments

Update

Thanks to answers provided, I've been reading and I've learned a few things. The gist of it is that I'm using a stock Ubuntu installation of Apache2 and I haven't changed any configuration settings from the default. So, none of the suggested causes are set.

Of course there is the chance that I've misunderstood some of the documentation. This is the first time I've read about some of these options.

Multiviews
Multiviews provides the best explanation of this scenario, but that requires a type map or MultiViews option set for a path. I only have one type map (/etc/apache2/mods-enabled/mime.conf) for 'var' and the MultiViews option is only set for /usr/share/apache2/icons. So, this couldn't be it.

Redirect & RedirectMatch
Redirect & RedirectMatch would provide a good explanation for this behavior, but it requires a directive for each redirection. I don't have any of these set and any path that I type serves the related php file if it exists. Also, if this was the cause, I think that the access.log would generate a 3xx status instead of 200 if it was a redirect.

Alias & AliasMatch
Alias & AliasMatch, like Redirect & RedirectMatch, requires a directive to be set for each resource. I only have an alias set for /icons/.

FallbackResource
FallbackResource identifies a single resource to serve when the requested resource isn't present. In my case, when the requested resource isn't present it serves the resource found by appending '.php' to the end of the request. I also double-checked and there are no FallbackResource directives set.

Here's an example of the command I use to confirm that there a particular option isn't set anywhere:

find /etc/apache2 -name "*.conf" -exec grep -li "FallbackResource" {} \;

Update #2:

There was a hole in my search for the MultiViews option. I was only looking for files with a .conf or .load extension, but some of the configuration files don't have an extension at all. The MultiViews option is set by the default site configuration file /etc/apache2/sites-enabled/000-default.

Thanks to @zneak for providing the answer first, although he did it in a comment. And thanks to @regilero who indirectly showed me what I was missing by using a different syntax for grep from the syntax I'm used to.

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

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

发布评论

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

评论(1

万水千山粽是情ミ 2025-01-03 02:10:37

您所说的重写可以通过许多其他指令来完成:

这些配置通常不在 .htaccess 文件中,因为 .htaccess 文件只是一件坏事,会减慢 apache 的速度,并允许用户添加一些除 VirtualHosts 中定义的规则之外的其他规则。东西在 /etc/apache2/sites-enabled/etc/apaches2/mod-enableds/ 和<代码>/etc/apache2/conf.d/。

现在最奇怪的是Option Multiviews创建一个grep -R MultiViews /etc/apache2/*。此选项会将任何对 http://www.example.com/foo/bar 的调用映射到foo/bar.php 或 foo/bar.html 如果这些目录存在于 DocumentRoot 中并且这些文件存在(尝试所有可用的扩展)。

What you call a rewrite can be done by a lot of others instructions:

Theses configuration things are usually not in .htaccess files by default, as .htaccess files is just a bad thing slowing down apache and allowing user to add some other rules than the one defined in VirtualHosts. Most usefull configuration stuff is in /etc/apache2/sites-enabled, /etc/apaches2/mod-enableds/ and /etc/apache2/conf.d/.

Now the strangest one is Option Multiviews make a grep -R MultiViews /etc/apache2/*. This option will map any call to http://www.example.com/foo/bar to foo/bar.php or foo/bar.html if theses directories exists in the DocumentRoot and if theses files exists (trying on all available extensions).

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