Discuss Apache content, file extensions, and persistent redirects

Given the Apache 2.x web server, which uses the Content Consol ( +MultiViews ) to access URLs without their extensions (for example, allow /foo vs. /foo.php or /foo.html ), as one of them produces 301 constant redirects when someone is really trying to use these extensions?

The goal is for all roads to expand the version of the sans URL, so /foo/ goes to /foo and /foo.html goes to /foo . This is the last thing that proves complexity. (Usage example. There are scattered legacy URLs on the Internet that still use the extension. We want them to be constantly redirected.)

There is a canonical link element, but even in the accompanying slides, it is better to make a proposal on the server side of the redirect in the first place.

I tried this with mod_rewrite , but it looks like Apache is "beating me to it" however. It is as if the extension is simply ignored. "No need, I covered it," says Apache. But then you cannot handle continuous redirects, and therefore expansion options without extension are allowed. Not the desired result. :)

Here is one example. Given a file name of 2-4 characters, consisting of lowercase letters and a test file placed in /foo/file.html , we want to redirect it to /foo/file forever:

 Options +MultiViews RewriteEngine on ... RewriteRule ^foo/([az]{2,4}).html/$ /foo/$1 [R=301,L] 

/foo/file/ and /foo/file.html/ redirect to /foo/file , but of course /foo/file.html not. If we try a rule similar to the following (note the absence of a trailing slash before $):

 RewriteRule ^foo/([az]{2,4}).html$ /foo/$1 [R=301,L] 

... we get too many redirects because Apache acts as if this rule were as follows, and therefore it ends its own tail:

 RewriteRule ^foo/([az]{2,4})$ /foo/$1 [R=301,L] 

In an attempt to be too smart for my own good, I also tried nested parentheses:

 RewriteRule ^foo/(([az]{2,4}).html)$ /foo/$2 [R=301,L] 

No dice. City loop redirection.

What would be really good is to capture such things "in bulk", so I don't have all these special cases floating in htaccess.

Another SO question began to consider this issue for one case of processing html files, but the proposed solution supposedly requires disabling Content Negotiation, which is not very good if you still want to use it for images and other file extensions (as in my case).

Extra credit. We also want to avoid the slash, so if someone tries /foo/ (which may itself be a .html or .php file), he goes to /foo no matter what. The first rule (above) fulfills this, but I think it is related to + MultiView. I have doubts about using DirectorySlash here, as there might be some risk that makes it less practical.

+4
source share

All Articles