In most cases 301 redirects are written in the “.htaccess” file. However, when the URL is complex and contains special characters it can be very tricky for the novice user.
To simplify the process we can redirect all the *.ASPX traffic to a PHP file, in which we will use a PHP script to perform the actual redirects.
First create an “.htaccess” file with a simple rewrite rule:
RewriteCond %{REQUEST_URI} (.*)\.aspx$ RewriteRule .* aspx_redirect.php [L]
Afterwards create the “aspx_redirect.php” file (which should be located in your public_html folder alongside the “.htaccess” file) and add the following code into it:
<?php $get_url = $_SERVER['REQUEST_URI']; $get_url = substr($get_url, 1); $redirect_to = array( 'Article.aspx?Item=626' => 'my-new-url', ); if (isset($redirect_to[$get_url])) { header("Location: /{$redirect_to[$get_url]}", true, 301); exit; } else { header("Location: /index.php", true, 301); exit; } ?>
Old url: Article.aspx?Item=626
New url: my-new-url
* Note that we are not using “/” (forward slash) at the beginning of each URL.
Tags: aspx, php
Leave a Reply