1

I need to migrate my old site URLs (ending with .aspx/asp and these are a few hundreds) to new site (ending with .jsp/servlets). We've opted to use RedirectMap and RedirectRule to implement this migration in Apache. The plain URLS are working fine. But case-sensitivity in the source URL is causing redirecting to a PAGENOTFOUND (For example, /magazine/Magazine.aspx?Id=4 is redirecting to PAGENOTFOUND instead of http://mydomain.com/advice/beauty).

Another issue is that it is unable to redirect the the source URLs with a query string (for example, /Help/Help.aspx?HelpID=6 is redirecting to PAGENOTFOUND instead of http://mydomain.com/services/essential-info).

Please find sample redirectmap text file below.

/100001/Product.aspx    http://mydomain.com/urlredirect/p100003
/100002/Product.aspx    http://mydomain.com/urlredirect/p100005
/Help/Help.aspx?HelpID=6    http://mydomain.com/services/essential-info
/Help/Help.aspx?HelpID=11   http://mydomain.com/services/info-about-delivery-methods
/Magazine/Feature.asp?Id=816    http://mydomain.com/advice/all-about-you.asp
/Magazine/Magazine.aspx?Id=4    http://mydomain.com/advice/beauty
/Advive/all-about-you.asp   http://mydomain.com/advice/beauty-in-you

Redirect code written in Apache

RewriteEngine on
RewriteMap text2id txt:/opt/webserver/apache/conf/redirectmap.txt
RewriteRule ^(.*\.(aspx|asp)) ${text2id:$1} [NC,R=301,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Chuneel
  • 41
  • 5

3 Answers3

3

Jon Lin's answer helped me to search for the correct redirect in the map. We've updated the code as below.

Below is the redirect code implemented.

RewriteEngine on
RewriteMap text2id txt:/opt/webserver/apache/conf/redirectmap.txt
# Base URL Does have a QS 
RewriteCond %{REQUEST_URI} ^(.*\.(aspx|asp).*)
RewriteCond ${text2id:%1?%{QUERY_STRING}} ^(.*\?.*)$
RewriteRule ^.*$ %1 [R=301,L]

# Base URL exists in the map
RewriteCond %{REQUEST_URI} ^(.*\.(aspx|asp).*)
RewriteRule ^.*$ ${text2id:%1|PAGENOTFOUND} [R=301,L]
Chuneel
  • 41
  • 5
1

You're not going to be able to match against the query string like that.

In your rule:

RewriteRule ^(.*\.(aspx|asp)) ${text2id:$1} [NC,R=301,L]

The only thing being sent to the rewrite map is the URI path, not the query string. You need to pass the query string into the rewrite map as well:

RewriteRule ^(.*\.(aspx|asp)) ${text2id:$1?%{QUERY_STRING}} [NC,R=301,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks Jon Lin. But above solution is breaking for plain URL redirects, such as `/100001/Product.aspx` is redirecting to `PAGENOTFOUND`. I tried below logic too. `RewriteRule ^(.*\.(aspx|asp).*) ${text2id:$1?%{QUERY_STRING}|$1} [R=301] RewriteRule ^(.*\.(aspx|asp)) ${text2id:$1} [R=301]` – Chuneel Sep 21 '12 at 11:47
  • Nope. The rule isn't working for both redirect maps, one without QS and one with QS. – Chuneel Sep 28 '12 at 13:58
0

If it's not working for you, try double percent in front of your QUERY_STRING...

As additional information, my colleague and I struggled with matching on QUERY_STRING within a redirect map file for most of the day. We are running Helicon on Windows, so not sure if that has anything to do with our solution being subtly different than the one above, but figured it may help someone else in our situation ...

Our issue is that the above RewriteRule was not properly parsing the QUERY_STRING in our environment. The resulting URL appeared with URL encoded characters and the word QUERY_STRING (like %7bQUERY_STRING%7d).

For any references to the QUERY_STRING within the rewrite map lookup string, we ended up having to double escape the query string environment variable. For instance, in our httpd.conf file, the above answer (thanks for the answer by the way) ended up looking like this:

RewriteRule ^(.*\.(aspx|asp)) ${text2id:$1?%%{QUERY_STRING}} [NC,R=301,L]

Note that the QUERY_STRING environment variable is preceeded by double percent signs (%%) rather than a single. This is the only way we could get the engine to parse the query string nested inside the rewrite map lookup reference. This was true for RewriteCond in our environment as well.

This may not apply to everyone, but if you are seeing your redirect URL contain something like %7bQUERY_STRING%7d, then you may want to give it a try. It may save you hours of pain.

Best, Brandon

BrandonP72
  • 33
  • 1
  • 6