没有mod_rewrite的Apache重写? [英] Apache rewriting without mod_rewrite?

查看:22
本文介绍了没有mod_rewrite的Apache重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

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

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.

Apache 配置是 Ubuntu 的库存配置,我怀疑这是 Ubuntu 独有的配置,但我找不到原因.

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.

以下是我的一些观察:

  • /etc/apache2/httpd.conf 是空的,但配置在/etc/apache2/apache2.conf 中,它又Includes 来自其他几个目录的配置文件.
  • 在此站点的层次结构中的任何位置都没有 .htaccess 文件.我检查了从/var/www
  • 开始的每个目录
  • /etc/apache2 下的 .conf 或 .load 文件除了/etc/apache2/mods-available 中的 rewrite.load 文件外,没有任何参考重写.
  • 我已将 LogWarning 指令设置为调试.当我访问没有 .php 扩展名的页面时,错误日志显示带有扩展名的 URL,访问日志显示它没有扩展名和 HTTP 200 状态.该页面已正确提供.
  • 如果我确实添加了具有适当配置的 .htaccess 文件,则会收到 .htaccess: Invalid command 'RewriteEngine' 错误....默认情况下甚至没有加载 mod_rewrite.
  • 如果我通过链接到/etc/apache2/mods-enabled 中的加载文件来启用 mod_rewrite,它会按预期工作,也就是说与我没有启用它时完全一样.
  • /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 显示:

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"

虽然 error.log 显示:

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/

如果我添加一个 .htaccess 文件:

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

更新

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

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 选项.我只有一种类型映射 (/etc/apache2/mods-enabled/mime.conf) 用于var",并且 MultiViews 选项仅为/usr/share/apache2/icons 设置.所以,这不可能.

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.

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

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.

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

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 标识要在请求的资源不存在时提供的单个资源'目前.在我的情况下,当请求的资源不存在时,它通过将.php"附加到请求的末尾来提供资源.我也仔细检查过,没有设置 FallbackResource 指令.

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" {} ;

更新 #2:

我在寻找 MultiViews 选项时遇到了一个漏洞.我只是在寻找带有 .conf 或 .load 扩展名的文件,但有些配置文件根本没有扩展名.MultiViews 选项由默认站点配置文件/etc/apache2/sites-enabled/000-default 设置.

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.

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

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.

推荐答案

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

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

默认情况下,这些配置内容通常不在 .htaccess 文件中,因为 .htaccess 文件只是一件坏事,会降低 apache 的速度并允许用户添加一些其他规则而不是 VirtualHosts 中定义的规则.最有用的配置内容在 /etc/apache2/sites-enabled/etc/apaches2/mod-enableds//etc/apache2/conf.d/.

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/.

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

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).

这篇关于没有mod_rewrite的Apache重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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