通过.htaccess将旧ASP页面301重定向至WordPress PHP页面
Alright, let's figure out how to get those 301 redirects working from your old ASP pages to your new WordPress site. I've dealt with plenty of these migrations before, so here are the most reliable methods depending on your server setup:
If your WordPress site is hosted on an Apache server, editing the .htaccess file in your site's root directory is the most straightforward approach. Here's how to set up the specific redirect you need:
- Connect to your server via FTP or cPanel's File Manager, locate the
.htaccessfile (it might be hidden, so enable viewing hidden files). - Add the following code before any existing WordPress rewrite rules (usually starting with
# BEGIN WordPress):
RewriteEngine On # Redirect specific ASP page with query parameters to WordPress product URL RewriteCond %{QUERY_STRING} ^ptid=20041&ID=15393$ RewriteRule ^details\.asp$ /product/numbershark5/? [R=301,L]
RewriteCondmatches the exact query string from your old ASP link.RewriteRuletargets thedetails.aspfile and redirects it to your new WordPress product URL.- The trailing
?removes the old query parameters from the redirected URL. R=301specifies a permanent redirect (critical for SEO), andLtells Apache to stop processing further rules after this one.
Since ASP sites often run on IIS, you'll need to use the URL Rewrite module (make sure it's installed on your server first):
- Open your site's
web.configfile (in the root directory). - Add the following rewrite rule inside the
<system.webServer>section:
<configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect ASP to WordPress" stopProcessing="true"> <match url="^details\.asp$" /> <conditions> <add input="{QUERY_STRING}" pattern="^ptid=20041&ID=15393$" /> </conditions> <action type="Redirect" url="/product/numbershark5/" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
stopProcessing="true"acts like theLflag in Apache, preventing other rules from running after this one.redirectType="Permanent"sets the 301 status code for SEO.
If you don't have direct access to server files, use a WordPress plugin to handle the redirect. The Redirection plugin is the most popular and reliable option:
- Install and activate the Redirection plugin from your WordPress dashboard.
- Go to Tools > Redirection and click the "Add New" button.
- In the "Source URL" field, enter:
/details.asp?ptid=20041&ID=15393 - In the "Target URL" field, enter:
/product/numbershark5/ - Set the "Redirect Type" to 301 - Permanent.
- Click "Add Redirect" to save the rule.
Quick Testing Tips
- After setting up the redirect, test it using a tool like
curl(runcurl -I http://www.edtech.ie/details.asp?ptid=20041&ID=15393in your terminal) to confirm you get a301 Moved Permanentlystatus code. - Browsers often cache redirects, so use an incognito window or clear your browser cache when testing.
- If you have dozens/hundreds of these redirects, you can create a CSV file with source/target pairs and import it via the Redirection plugin, or write bulk rewrite rules in
.htaccess/web.config.
内容的提问来源于stack exchange,提问作者Crashy

