homepage Welcome to WebmasterWorld Guest from 54.205.99.71
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewrite or Redirect submaps to submaps
Diniz




msg:4577580
 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
www.example.com/sub-map/sub-sub-map/

How can I do this?

 

lucy24




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

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.

Diniz




msg:4577607
 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]

lucy24




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

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 ::

Diniz




msg:4577697
 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.

lucy24




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

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
www.example.nl
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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved