System using mapfiles, how do I apply redirects for old page

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

System using mapfiles, how do I apply redirects for old page

24 Apr 2012, 13:20

Hi, the system I work with is using mapfiles, and now I need to redirect old (google indexed) pages. Which rules is it possible to add to the .htaccess?

I wish to redir an old page that has for example the url:
/display.asp?id=32 (old page) to the new page:
/se/my-new-page/

and of course all other pages with the url: /display.asp?id=xx as well ...

Is this possible to accomplish? And in that case, how?

The map-file (default_se.txt) in the new system looks like this
=======================================
home 1
my-new-page 2 <-- this is the "pretty-url" I wish to redirect the old display.asp?id=32 to


This is the .htaccess file, that I wish to modify with the the new rules added
=========================================================
RewriteEngine on
RewriteBase /

## MAP FILE SOLUTION
RewriteMap default_se txt:mfiles/default_se.txt
RewriteMap default_en txt:mfiles/default_en.txt
RewriteMap default_de txt:mfiles/default_de.txt

RewriteMap news_se txt:db/mfiles/news_se.txt
RewriteMap news_en txt:db/mfiles/news_en.txt
RewriteMap news_de txt:db/mfiles/news_de.txt

RewriteMap lower int:tolower

# default
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${default_se:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${default_se:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${default_en:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${default_en:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${default_de:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${default_de:${lower:%2}}&lng=%1 [NC,L]

# news
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${news_se:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${news_se:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${news_en:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${news_en:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${news_de:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${news_de:${lower:%2}}&lng=%1 [NC,L]

Kind Regards
/ Anette

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 06:45

Hello,

In case you want a redirect rule, than I'd suggest smth like the following:

Code: Select all
RewriteMap display_se txt:mfiles/display_se.txt

RewriteCond %{QUERY_STRING} ^id=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /se/${display_se:%1}? [NC,R=301,L]


mapfile would contain:

12 new-page
13 other-page
23 something_else



Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 08:18

Hi Andrew, and thank you for taking your time :-)

The rule works ... almost

I implemented the rule in the .htaccess and made a mapfile (display_se)

3 about-us

But where it should display, ex. www.mydomain.com/se/about-us/ <-- slash at the end of the url

it only goes to
www.mydomain.com/se/about-us <-- no slash and thus I get a not-found page.

Kind regards
/ Anette

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 08:22

Adjust the rule:

Try using:
Code: Select all
Rewriterule ^display\.asp$ /se/${display_se:%1}/? [NC,R=301,L]

instead of:
Code: Select all
Rewriterule ^display\.asp$ /se/${display_se:%1}? [NC,R=301,L]


Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 08:42

Sorry, the first rule worked, I made a typo :-)

I.e.
This works

# MAPFILE
RewriteMap display_se txt:mfiles/display_se.txt


# REDIR TEST
RewriteCond %{QUERY_STRING} ^id=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /se/${display_se:%1}? [NC,R=301,L]

But I came to think of that I have english and german pages that needs to be redirected,
the url for these are, for example:

/display.asp?id=3&lang=en

I realize that I need seperate mapfiles for these, which is not a problem, but how do I add the second parameter (lang=xx) to the rule?

Kind Regards
/ Anette

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 09:00

How about this one?

Code: Select all
# MAPFILE
RewriteMap display_se txt:mfiles/display_se.txt


# REDIR TEST
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /se/$2/${display_se:%1}? [NC,R=301,L]


Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 09:02

Sorry again - the second rule, for the first example (i.e. pages without the parameter lang=xx) is the one that is working, I had added a slash after the page name in the display_se mapfile like:

3 om-oss/

and that's why the first rule you provided didn't work.

This is the working rule:
Rewriterule ^display\.asp$ /se/${display_se:%1}/? [NC,R=301,L] <-- with the slash before the question mark.

and the mapfile looks like this:
3 om-oss

I will try the rule you provided for the english and german urls and let you know asap :-)

Kind regards
/ Anette
Last edited by Anejo on 26 Apr 2012, 09:39, edited 1 time in total.

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 09:20

Hi Andrew, when using this rule:

RewriteMap display_en txt:mfiles/display_en.txt

RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /en/$2/${display_en:%1}/? [NC,R=301,L] <-- added slash here

The result is:
/en//about-us/ <-- i.e. one slash to many between "en" and "about-us"

The mapfile display_en looks like this:
3 about-us

The mapfile display_se looks like this:
3 om-oss

Could it be a problem that the same id is used in the two mapfiles, even if the parameter behind (lang=xx) retrieves info from different db-tables?
(i.e. tbl_pages_se, and tbl_pages_en) and when the parameter "lang=xx" is omitted, the system retreives from the tbl_pages_se by default.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 10:14

Well,

Two slashes is the result that lang= was empty. Simply make another rule with one parameter having null.
there shouldn't be any problems with two different entries in two different files.

Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

26 Apr 2012, 12:06

OK, could it have something to do with that in the old system the querystring parameter is lang=xx and in the new system the qs-parameter is lng=xx?

Regards
/ Anette

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

27 Apr 2012, 06:12

Hi Andrew, I'm sorry but I can't get the rules for pages with the lang=xx parameter working and I've tried adding a second rule for the case lang-parameter null, but I'm not sure how to write that rule (I've tried several scenarios that didn't work - so I removed the rule again).

I call the pages like this:
/display.asp?id=3&lang=en <-- i.e. the langparameter is not empty

The complete .htaccess looks like this now
==========================
Code: Select all
RewriteEngine on
RewriteBase /

## OLD SYSTEM MAPFILES
RewriteMap display_se txt:mfiles/display_se.txt
RewriteMap display_en txt:mfiles/display_en.txt

## NEW SYSTEM MAPFILES
RewriteMap pages_se txt:mfiles/pages_se.txt
RewriteMap pages_en txt:mfiles/pages_en.txt
RewriteMap pages_de txt:mfiles/pages_de.txt

RewriteMap calendar_se txt:mfiles/calendar_se.txt
RewriteMap calendar_en txt:mfiles/calendar_en.txt
RewriteMap calendar_de txt:mfiles/calendar_de.txt

RewriteMap lower int:tolower

# NEW SYSTEM - RULES FOR MAPPINGS
# Pages
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${pages_se:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${pages_se:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${pages_en:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${pages_en:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${pages_de:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${pages_de:${lower:%2}}&lng=%1 [NC,L]

# Calendar
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${calendar_se:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${calendar_se:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${calendar_en:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${calendar_en:${lower:%2}}&lng=%1 [NC,L]

RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${calendar_de:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${calendar_de:${lower:%2}}&lng=%1 [NC,L]

# OLD SYSTEM - RULES FOR REDIRECTION TO NEW SYSTEM MAPFILES
# SWEDISH <-- This rule works!
RewriteCond %{QUERY_STRING} ^id=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /se/${display_se:%1}/? [NC,R=301,L]

# ENGLISH <--- This rule doesn't work I'ts here I get the /en//about-us/
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /en/$2/${display_en:%1}/? [NC,R=301,L]


The Mapfiles for the english pages looks like this

Mapfile for the new pages: pages_en.txt
----------------------------------------------
Code: Select all
3 about-us
5 our-products
6 contact-us


Mapfile for redirect to new pages: display_en.txt
------------------------------------------------------
Code: Select all
9   about-us
15 our-products
33 contact-us


Regards
/ Anette

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

27 Apr 2012, 07:53

Hello,

of course!!!! It's a mistake - $2, while it should be %2 in the following:

Code: Select all
# ENGLISH
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /en/$2/${display_en:%1}/? [NC,R=301,L]


Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

27 Apr 2012, 10:40

Good morning Andrew (and thank you for your patience) :-)

I changed the rule as follows:

RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /en/%2/${display_en:%1}/? [NC,R=301,L]

and now I get one /en/ to many, like this:
/en/en/about-us/

Regards
/ Anette

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

27 Apr 2012, 10:47

I gave that rule as a sample, so why don't you use:


Code: Select all
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /%2/${display_en:%1}/? [NC,R=301,L]

Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

27 Apr 2012, 11:37

Hi sorry, I didn't understand, these rules are so tricky to grasp and I thought the /en/ was required for this to work :(

But the english rule works now, a million thanks Andrew!!!

I also think that there's some cashing or something involved here, because when I test the rules it's like they don't begin to work immediately.
And I think the cashing is on the server as I empty my browser caches on a regular basis, and I also test in several different browsers.

Now I've copied the english rule and modified it for the german pages (id=x&lang=de), I guess I have to be patient and wait for a while to see if
it works or not because right now the display.asp?id=5&lang=de goes to /de/about-us/ <-- instead of /de/uber-uns/ (as of the german mapfile display_de).

Cheers, and once again - I'm so very grateful for your patience and your help!
/ Anette

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

28 Apr 2012, 04:33

Hi Andrew,

I'm beginning to believe that the rules for the new system mapfiles interferes with the redirect rules?
(and the caching had to do with MSIE 9, ctrl+5 clears the cache, I used F5 which simply reloads the page without clearing the cache).

The rule for the german pages works kind of now, but it picks the english title:
/de/membership-info/ instead of /de/uber-uns/ for the redirect url:

/display.asp?id=4&lang=de (i.e. the old url that is to be directed to the new system url)

THE ENGLISH MAPFILE: display_en (for redirecting the old urls)
2 home
3 about-us
4 membership-info
5 preventive-advice

THE ENGLISH MAPFILE: pages_en (for the new system urls)
search 1
home 2
about-us 3
membership-info 4
preventive-advice 5

Please note! It's a coincident that the id:s for the new and old english urls are all the same!

THE GERMAN MAPFILE: display_de (for redirecting the old urls)
2 startseite
3 mitgliedschaft
4 uber-uns
5 ratschlage-fur-ihre-reise

THE GERMAN MAPFILE: pages_de (for the new system urls)
suche 1
startseite 2
uber-uns 3
mitgliedschaft 4
ratschlage-fur-ihre-reise 5

Please note! For the german pages, the old and new id:s are not all the same!

The .htaccess
----------------
RewriteEngine on
RewriteBase /

## REDIRECT MAPPFILES
RewriteMap display_se txt:mfiles/display_se.txt
RewriteMap display_en txt:mfiles/display_en.txt
RewriteMap display_de txt:mfiles/display_de.txt

## NEW SYSTEM MAPFILES
RewriteMap pages_se txt:mfiles/pages_se.txt
RewriteMap pages_en txt:mfiles/pages_en.txt
RewriteMap pages_de txt:mfiles/pages_de.txt

RewriteMap calendar_se txt:mfiles/calendar_se.txt
RewriteMap calendar_en txt:mfiles/calendar_en.txt
RewriteMap calendar_de txt:mfiles/calendar_de.txt

RewriteMap lower int:tolower

# Default pages
# SWEDISH
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${pages_se:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${pages_se:${lower:%2}}&lng=%1 [NC,L]

# ENGLISH
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${pages_en:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${pages_en:${lower:%2}}&lng=%1 [NC,L]

# GERMAN
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${pages_de:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /default.asp?id=${pages_de:${lower:%2}}&lng=%1 [NC,L]

# Calendar pages
# SWEDISH
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${calendar_se:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${calendar_se:${lower:%2}}&lng=%1 [NC,L]

# ENGLISH
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${calendar_en:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${calendar_en:${lower:%2}}&lng=%1 [NC,L]

# GERMAN
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/.]+)/$ [NC]
RewriteCond ${calendar_de:${lower:%2}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /cal.asp?id=${calendar_de:${lower:%2}}&lng=%1 [NC,L]

# REDIRECT RULES
# SWEDISH
RewriteCond %{QUERY_STRING} ^id=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /se/${display_se:%1}/? [NC,R=301,L]

# ENGLISH
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /%2/${display_en:%1}/? [NC,R=301,L]

# GERMAN
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /%2/${display_de:%1}/? [NC,R=301,L]

Kind Regards
/ Anette

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

03 May 2012, 05:20

Well, it's getting a little complicated now. So I suggest making a condition as in following:

Code: Select all
# GERMAN
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=[b](de)[/b]$ [NC]
Rewriterule ^display\.asp$ /%2/${display_de:%1}/? [NC,R=301,L]


So we hardcode this parameter to be german and use ONLY with display_de. Of course, you can adjust it for your situation. It's just a hint.

Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

04 May 2012, 10:08

Sorry, tried this and it still uses the english mapfile-titles, even if it goes to the german pages
or vice versa... I'll do some more testing though to see if the problem lies elsewhere.

Once again, thank you for your support and your patience :-)

Kind Regards
/ Anette

PS! Just checking, is it correct that I should remove the previous rule for the german pages, and
replace it with the new none you provided, i.e:

This is being removed:
Code: Select all
# GERMAN
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=([^&]+)$ [NC]
Rewriterule ^display\.asp$ /%2/${display_de:%1}/? [NC,R=301,L]


and replaced with:

Code: Select all
# GERMAN
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=[b](de)[/b]$ [NC]
Rewriterule ^display\.asp$ /%2/${display_de:%1}/? [NC,R=301,L]


I have tried every combination without success, i.e. keeping both rules, removing the old rule and replacing it with the new rule.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: System using mapfiles, how do I apply redirects for old

07 May 2012, 06:47

Hello,

I hope you understand that and are the formatting of the text in forum to highlight the changed part and you shouldn't paste it in config file.
there're also 2 options: move this rule on top of the English rule, OR apply the same changes to the english rule and insert EN istead of DE.

Regards
Andrew

User avatar
Posts: 23
Joined: 24 Apr 2012, 13:14

Re: System using mapfiles, how do I apply redirects for old

13 May 2012, 09:11

Hi Andrew,

Just wanted to report the solution that worked, these rules works perfectly.

For the English pages:

Code: Select all
# ENGLISH
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=(en)$ [NC]
Rewriterule ^display\.asp$ /%2/${display_en:%1}/? [NC,R=301,L]


and for the german pages

Code: Select all
# GERMAN
RewriteCond %{QUERY_STRING} ^id=([^&]+)&lang=(de)$ [NC]
Rewriterule ^display\.asp$ /%2/${display_de:%1}/? [NC,R=301,L]


Than you very much for your help and your patience!

Kind Regards
/ Anette

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 1 guest