This past weekend, I ran into a redirect problem I have never seen before.

BACKGROUND: the site is a static site that was created by the site owner in a small, remote town. She figured out HTML and CSS amazingly well, but the file structure and CSS are poorly organized. So, my main task in helping her is to reorganize both while improving SEO. As I create a directory structure and move pages into the directories, I am adding 301 redirects to the .htaccess file. Then I test that the redirect works before removing the original file from the server.

ACTION: The first file move was to create a folder for a series of information pages about different minerals and rocks. These pages are not mission critical and made a good test. The first step was to create a directory called MineralFinder with a file called MineralFinderHome.html. The original file was at the root level and called minfind.htm.

BEHAVIOR: The first redirect line worked just fine. The second - fourth lines were

Redirect 301 /aluminum http://example.com/MineralFinder/Aluminum
Redirect 301 /aluminum/aluminum.htm http://example.com/MineralFinder/Aluminum/Aluminum.html
Redirect 301 /aluminum/process.htm http://example.com/MineralFinder/Aluminum/HowAluminumIsProcessed.html

These caused a URL not found error:

Not Found error

as shown in the image. The weird thing is that it is referencing the old file name without the camel case, but keeping the new directory structure with the camel case.

TECH SUPPORT: Tech support basically said they don't know why it's a problem and to use CPanel redirects. I'm not satisfied with that answer and want to know the cause of the behavior.

1 Answer 1

Redirect 301 /aluminum ....
Redirect 301 /aluminum/aluminum.htm ....
Redirect 301 /aluminum/process.htm ....

The mod_alias Redirect directive is prefix matching, so the first directive here will catch both /aluminum/aluminum.htm and /aluminum/process.htm. AND everything after the match is appended onto the end of the target URL.

So, given a request for /aluminum/aluminum.htm. This matches the first directive, and /aluminum.htm (the URL-path after the match) is appended onto the target http://example.com/MineralFinder/Aluminum which results in the URL in your screenshot, ie. http://example.com/MineralFinder/Aluminum/aluminum.htm.

The solution is to reverse these directives so the more specific directives are first.

You will need to clear your browser cache before testing, as the earlier 301 will be cached hard by the browser. (It is often safer to test with a temporary 302 redirect as this avoids the browser cache.)

Or, use RedirectMatch which matches against a regex and is not prefix matching. For example:

RedirectMatch 301 ^/aluminum$ http://example.com/MineralFinder/Aluminum

With the start and end anchors (^ and $ respectively) this only matches a request for /aluminum and not /aluminum/aluminum.htm.

Tech support basically said ... to use CPanel redirects.

If you are happy editing .htaccess files then I would avoid using "cPanel redirects". If you have other directives in .htaccess then I would advise against using "cPanel redirects". And if you need to do anything slightly more complex then... you can't use "cPanel redirects". (Btw, "cPanel" simply edits the .htaccess file when it implements a redirect, but it doesn't always get it right.)

  • That worked like a charm! I removed the tech support's entries in CPanel, rearranged the order of my redirects, and re-uploaded the .htaccess file. It would be more efficient to use rewrite rules, but I'm trying to find a system that my client can use on her own when the re-organization is finished. She's a smart lady, but I think that regular expressions would be over the top for her. I was wondering where CPanel was storing the redirects. I was hoping they weren't in a database. Mar 17, 2017 at 1:45
  • 1
    "It would be more efficient to use rewrite rules" - Well, it's arguably "more efficient" to use Redirect (or RedirectMatch) like you are doing. However, you would consider using a mod_rewrite RewriteRule (which is what I assume you are referring to) if you have existing mod_rewrite directives - this is simply to avoid conflicts. (Note that Redirect and RedirectMatch both belong to mod_alias.)
    – MrWhite
    Mar 17, 2017 at 2:11
  • This is a case were starting with the general and pushing the specifics down doesn't work. Mar 17, 2017 at 14:53
  • 1
    Aside: Since /MineralFinder/Aluminum would seem to be a physical directory, I would consider redirecting to /MineralFinder/Aluminum/ ie. with a trailing slash (although this depends on what the old links are like. ie. /aluminum or /aluminum/?) If you don't and you request /aluminum (no trailing slash) then mod_dir will issue a second redirect to "fix" the URL by appending a slash. Two redirects instead of one. A minor thing really, just something to consider.
    – MrWhite
    Mar 17, 2017 at 17:07
  • I wondered about that and will do it! Mar 18, 2017 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.