2008-10-17 Apache Rewrite Adds Slashes

Hm... I have an Apache Rewrite problem. Does anybody understand the following? What I wanted to was this:

If somebody visits `/wiki/Foo` the request is rewritten to `/cgi-bin/alex/Foo`. This works.

If somebody visits `/wiki?action=foo` the request is rewritten to `/cgi-bin/alex/?action=foo`. Notice the extra slash.

Why is that? Somebody is sending a **301 Moved Permanently** response. But who?

I’m starting at the RewriteRule documentation and searching for the word “redirect” trying to make sense of it all. The only passage that seems to be relevant:

RewriteRule

If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL.

Grrrr! I must have introduced something when I fiddled with .htaccess, recently.

And I’ve been getting these for years now. If only I knew what caused them:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use ‘LimitInternalRecursion’ to increase the limit if necessary. Use ‘LogLevel debug’ to get a backtrace.

​#Oddmuse ​#Wikis ​#Web

Comments

(Please contact me if you want to remove your comment.)

I need to look into this. There is no such problem on the CommunityWiki site, for example. But even after investing quite some time and rewriting my .htaccess file multiple times, I’m no closer to a solution than before.

– Alex Schroeder 2008-10-18 09:11 UTC

Alex Schroeder

---

Taking a look at this, I have to ask what does the following?

`https://alexschroeder.ch/wiki` → `https://alexschroeder.ch/wiki/`

I think that redirect is inserting the slash, then your redirect for `cgi-bin` is working.

– AaronHawley 2008-10-21 20:09 UTC

AaronHawley

---

Hm, could it be that zealous Apache admins are using a vhost config or server config to do that? I don’t see any rule in my htdocs/.htaccess that would add such a slash...

– Alex Schroeder 2008-10-21 22:24 UTC

Alex Schroeder

---

Its just something Apache has always done:

“Description:

“Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say `/~quux/foo` instead of `/~quux/foo/` then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc. Solution:

“The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, [...]”¹

¹

– AaronHawley 2008-10-22 15:06 UTC

AaronHawley