Redirect from ASPX to Wordpress

I am trying to redirect [B]http://theitinerantgolfer.com/Courses.aspx?ID=218[/B] to [B]http://theitinerantgolfer.com/pinehurst-resort-no-2-course/[/B]

I have around 100 golf course posts to redirect so any help would be appreciated.

My permalinks are set up as: /%postname%/

So far I have had my htaccess file set up the following ways, but I keep getting a 404 page.

RewriteEngine On
RewriteRule ^(.+)\\.aspx?$ $1.php [L]

RewriteCond %{HTTP_HOST} =theitinerantgolfer.com/Courses.aspx
RewriteCond %{QUERY_STRING} =id=218
RewriteRule ^default\\.asp$ http://theitinerantgolfer.com/pinehurst-resort-no-2-course/ [NC,L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

[HR][/HR]

RewriteCond %{HTTP_HOST} =theitinerantgolfer.com/Courses.aspx
RewriteCond %{QUERY_STRING} =id=218
RewriteRule ^default\\.asp$ http://theitinerantgolfer.com/pinehurst-resort-no-2-course/ [NC,L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

[HR][/HR]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect 301 /Courses.aspx?ID=218 http://theitinerantgolfer.com/pinehurst-resort-no-2-course/

Hi, hg!

Seriously? Is your aspx website hosted on an Apache server or did you just move your domain to Apache?

Okay, believing that it must be the latter situation, where is Apache supposed to get “pinehurst-resort-no-2-course” from? Because mod_rewrite does not have access to a database (at least most webmasters won’t), you’ll not be able to translate ID=218 directly to pine… What you’re asking for is either the RewriteMap (which is only available via httpd.conf or httpd-vhosts.conf) or using a handler script to do the database lookup for you then redirect to pine… In either case, I believe you need to study mod_rewrite a bit before proceeding (you can start with the link in my signature).

Saying that, you should not get a 404 on redirecting Courses.aspx to Courses.php unless Courses.php does not exist.

WARNING TO OTHERS: Do NOT use code like this!

RewriteEngine On
RewriteRule ^(.+)\\.aspx?$ $1.php [L]

RewriteCond %{HTTP_HOST} [COLOR="#FF0000"]=[/COLOR]theitinerantgolfer.com/Courses[COLOR="#0000FF"]\\[/COLOR].aspx
RewriteCond %{QUERY_STRING} [COLOR="#FF0000"]=[/COLOR]id=218
RewriteRule ^default\\.asp$ http://theitinerantgolfer.com/[COLOR="#800080"]pinehurst-resort-no-2-course[/COLOR]/ [NC,L,R=301]

# BEGIN WordPress
[COLOR="#FF0000"]<IfModule mod_rewrite.c>[/COLOR]
[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing
 expecting a different result.  Asking Apache to confirm the existence of ANY module with
 an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. 
 DON'T BE AN IDIOT!  If you don't know whether a module is enabled, run the test
 ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache's resources
 (and should NEVER be allowed on a shared server).[/indent][/rant 4]
# WordPress has an excuse: They don't want to bring down an idiot's website
# if mod_rewrite is not available. You do NOT have any excuse to abuse a server
# like this!
RewriteEngine On
[COLOR="#FF0000"]RewriteBase /[/COLOR]
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="#FF0000"]</IfModule>[/COLOR]
# END WordPress

[HR][/HR]

RewriteCond %{HTTP_HOST} =theitinerantgolfer.com/[COLOR="#FF0000"]Courses.aspx[/COLOR]
RewriteCond %{QUERY_STRING} [COLOR="#FF0000"]=[/COLOR]id=218
RewriteRule ^default\\.asp$ http://theitinerantgolfer.com/[COLOR="#800080"]pinehurst-resort-no-2-course[/COLOR]/ [NC,L,R=301]

# BEGIN WordPress
[COLOR="#FF0000"]<IfModule mod_rewrite.c>
# Really??? Do I need to repeat my rant from above?[/COLOR]
[COLOR="#FF0000"]RewriteEngine On[/COLOR]
[COLOR="#FF0000"]RewriteBase /[/COLOR]
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="#FF0000"]</IfModule>[/COLOR]
# END WordPress

[HR][/HR]

# BEGIN WordPress
[COLOR="#FF0000"]<IfModule mod_rewrite.c>
# Again???[/COLOR]
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="#FF0000"]</IfModule>[/COLOR]
# END WordPress

Redirect 301 /Courses.aspx?ID=218 http://theitinerantgolfer.com/pinehurst-resort-no-2-course/

Regards,

DK

Hi DK, thanks for replying to my post.

I should have noted in my original post that I have no experience with this. I know HTML, CSS and can make educated guesses at altering Wordpress code (I am more of a designer than developer). The code examples above were from reading articles after searching “Redirect ASP to Wordpress”, “Redirect ASP to PHP” or “Redirect Dynamic ASP to Wordpress”.

The ASP site was hosted at GoDaddy (windows server) but has switched to Wordpress at Hostgator (Linux Server).

Courses.php does not exist.

Are you saying that I should not be altering htaccess but httpd.conf or httpd-vhosts.conf?

I do not see these files in the directory, would I just make them?

Happy holidays to you.

Hi hg!

Thanks for not taking offense at my Standard Rant #4.

The point of my response (and reference to RewriteMap) is that is the only way that mod_rewrite can help (without an inordinate number of ‘capture the query string and redirect somewhere unrelated’ directives). As a webmaster and not a sysadmin, you do NOT have access to the httpd.conf or your own httpd-vhosts.conf files (the httpd.conf is named differently - Apache2.conf - on a 'nix server) so you’re stuck with abusing the server with a long series of directives OR using a handler script which could be activated by a !-f RewriteCond (albeit that would have to be carefully crafted to avoid crippling WP’s !-f).

On second thought, ditch ALL your .htaccess code and replace with this:

RewriteEngine on

# handle Courses.aspx requests
# ignore the query string as it'll be passed through unaffected
RewriteRule ^Courses\\.aspx$ handler.php [L,N]
# The Last will prevent being ANDed with the WP code;
# The Next will allow the handler to accept the request and perform the redirection

# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php [L]
# Making the . optional will account for the empty filename, i.e., DirectoryIndex request
# END WordPress

You will then have to create the handler.php script which MUST NOT output any content! It’ll have to read your query string and determine the name of the redirection, i.e., pinehurst-resort-no-2-course - BUT DROP THE TRAILING / as that will upset WP! Then use PHP’s header(“Location: {insert redirection here}”); and handler will request the file from WP (via the .htaccess again).

Regards,

DK

hg,

<?php // handler.php

// initialize redirection map
$map= array(218 => 'pinehurst-resort-no-2-course');
// remainder of ID mappings

# initialize return value
$newURI = '';

# get ID as integer
if (isset($_GET['ID']))
{
    $ID = $_GET['ID'];
    if (isset($map[$ID])) $newURI =  $map[$ID];
}

# redirection 
header("Location:$newURI",TRUE,301); // OR header("Location:http://theitinerantgolfer.com/$newURI",TRUE,301);
?>

This code is untested but it should work for you (if you choose to use a handler file as recommended).

Regards,

DK