Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite or Redirect submaps to submaps



9:03 pm on May 24, 2013 (gmt 0)

I can rewrite the first directory of my domain with the following rule:

RewriteRule ^sub_map.*)$ /sub-map/$1 [R=301,L]

But I can't find the solution to rewrite a subdirectory.

This is what I want:

www.example.com/sub_map/sub_sub_map/ to

How can I do this?


9:55 pm on May 24, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Same way as your existing rule. Did you try something that didn't work? Probably a typo; let's have a look.

Are you redirecting a limited number of exact URLs? If not, you may be in php-detour territory unless everything follows the same naming formula, like /[a-z]+_[a-z]+/[a-z]+_[a-z]+_[a-z]+)

RewriteRule ^sub_map.*)$

I assume that's a typo too, or else the rule would never have worked and would probably result in an error.


10:07 pm on May 24, 2013 (gmt 0)

RewriteRule ^sub_map.*)$ /sub-map/$1 [R=301,L]

This rule really works, tested it serveral times. I tried something like this with the directory and subdirectory:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteBase /

RewriteRule ^sub_map.*)$ /sub-map/$1 [R=301,L]

RewriteRule ^sub_map/sub_sub_map.*)$ /sub-map/sub-sub-map$1 [R=301,L]

And also this one:

RewriteRule ^sub_map.*)$ /sub-map/$1 [R=301,L]

RewriteRule ^sub-map/sub_sub_map.*)$ /sub-map/sub-sub-map$1 [R=301,L]


1:19 am on May 25, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

RewriteRule ^sub_map.*)$ /sub-map/$1 [R=301,L]

This rule really works, tested it serveral times.

:: double-checking that this is the Apache subforum ::

We must now be in some variety of alternative reality, because I tried it on both MAMP and test site (live), substituting the names of two real directories for the "sub_map" and "sub-map" components but changing nothing else. Both ways I got the expected 500 error before I even got as far as trying a request that would meet the condition.

:: looking vaguely around for phranque or someone like him to shed light ::


9:09 am on May 25, 2013 (gmt 0)

::With all my cards open::

[shared-accountmanagers.nl ]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^shared-accountmanagers\.nl [NC]
RewriteRule ^(.*)$ http://www.shared-accountmanagers.nl/$1 [L,R=301]

RewriteBase /

#redirect voor /sub_map /sub-map
RewriteRule ^sub_map(.*)$ /sub-map/$1 [R=301,L]

I'm looking forward to hear from you what seems a typo in your opinion.


5:42 pm on May 25, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Phew. It WAS a typo. In the post, evidently, not in the real htaccess. You left out an opening parenthesis-- with disastrous results.

The rule you quoted first needs to go second-- in fact, it should be the very last redirect. Otherwise you risk having more than one redirect on a single request. Search engines frown on this-- and so do human users with slow connections, because when there are multiple redirects you start noticing the delay.

That was not your original question, but let me get it out of the way. It looks as if you want everyone to end up on
See Forums character about using example dot com for all domain names. This is a universal WebmasterWorld rule which goes double in the Apache subforum because we need to see what you typed. It can be example dot anything-- including example.xyz or things that don't really exist-- so you are welcome to say example dot your-own-country.

The RewriteCond should then say "if the requested host is anything other than exactly 'www.example.com' or exactly nothing (for HTTP/1.0 requests)", like this
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

Incidentally, your second-to-last redirect will usually be the one that redirects requests for explicit "/index.hmtl" (or whatever extension your files use) to / alone. Your specific rules all go earlier. You've already got

RewriteRule ^sub_map(.*)$ http://www.example.com/sub-map/$1 [R=301,L]

Always include the full protocol and domain in redirect targets so you don't run the risk of multiple redirects. This is especially important in your situation, where you're redirecting from an old form of a page name to a newer form. If people have incorrect bookmarks, they may be incorrect in more than one way.

And NOW, finally, we can get back to the original question. Are you redirecting a small number of pre-existing URLs that can be listed one by one in the htaccess? Or do you have vast numbers of URLs that used to contain lowlines and you're changing them all to hyphens?

The question is whether your rules should say

RewriteRule ^blah_blah/blah_blah_blah/(.*) http://www.example.nl/blah-blah/blah-blah-blah/$1 [R=301,L]

or more generically

RewriteRule ^([a-z]+)_([a-z]+)/([a-z]+)_([a-z]+)_([a-z]+)/(.*) http://www.example.nl/$1-$2/$3-$4-$5/$6 [R=301,L]

(Yes, technically you can combine $2 and $3, and again $5 and $6, but the potential confusion may not be worth it.)

Another question is: Do those old-style requests include index pages that might be called by name? If so, you need to make a separate rule just for them. Unlike the ordinary index redirect, this needs to come before the other lowline-to-slash redirects.

Featured Threads

Hot Threads This Week

Hot Threads This Month