Moving WordPress

Move WordPress from a subdirectory to website root

This example should work for any WordPress deployment, this article describes how to move WordPress on IIS 7 using ASP.Net

Benefits

  • Move WordPress
  • Preserve old links and permalinks
  • Preserve old images and uploads in posts and pages
  • Notify crawls of change with HTTP Response 301 Moved Permanently

This configuration will allow you to move WordPress to a different directory within your site and maintain you old links, permalinks, images, and uploads. All of the content will move with WordPress, this will just setup redirects to the new location. Each time an old link is used a HTTP Response 301 will be issued, this will help update search engines and backlinks. Custom HTTP Response 404 handling will be used to issue the HTTP Response 301.

Steps

  • Setup HTTP Response 301 Moved Permanently using ASP.Net (default.aspx)
  • Modify HTTP Response 404 Not Found handling using IIS 7 (web.config)
  • Modify rewrite rule to exclude old path using IIS 7 (web.config)
  • Update WordPress settings in database using MySQL
  • Update WordPress posts in database using MySQL

HTTP Response 301 Moved Permanently using ASP.Net
example default.aspx

<%@Page Language=”VB” %>
<%@Import Namespace=”System.Uri” %>
<%@Import Namespace=”System.Web” %>

<script runat=”server”>

Sub Page_Load(sender As Object, e As System.EventArgs)

Dim sUrl As System.String = HttpContext.Current.Request.RawUrl

Dim bChange As System.Boolean
bChange = False

If sUrl.IndexOf(“?404;”) > 0 Then
sUrl = sUrl.SubString(sUrl.IndexOf(“?404;”) + 5)
sUrl = sUrl.SubString(sUrl.IndexOf(“//”) + 2)
sUrl = sUrl.SubString(sUrl.IndexOf(“/”) + 1)
Else
sUrl = “”
End If

If sUrl.IndexOf(“blog/”) = 0 Then
sUrl = sUrl.SubString(5)
End If

Response.Status = “301 Moved Permanently”
Response.AddHeader(“Location”, “http://www.domain.com/” & sUrl)

End Sub

</script>

Web Server/IIS updates
example web.config


<?xml version="1.0" encoding="UTF-8"?/>
<configuration>
<system.webServer>
<httpErrors>
...
<error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/default.aspx" responseMode="ExecuteURL" />
...
</httpErrors>
<rewrite>
<rules>
<rule name="WordPress" stopProcessing="true">
<match url="/*" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="blog/*" matchType="Pattern" negate="true" />
</conditions>
<action type="Rewrite" url="/index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

WordPress updates
example MySQL synax


/* Update WordPress siteurl attribute */
update database_name.wp_options set option_value='http://www.domain.com' where option_name='siteurl';

/* Update WordPress home attribute */
update database_name.wp_options set option_value=’http://www.domain.com’ where option_name=’home';

/* Update WordPress upload_path atribute, this is a local path */
update database_name.wp_options set option_value=’C:\\Inetpub\\website\\wp-content\\uploads’ where option_name=’upload_path';

5 thoughts on “Moving WordPress

  1. Nice One

    I like your style, the fact that your site is a little bit different makes it so interesting, I get fed up of seeing same-old-same-old all of the time. I’ve just stumbled this page for you :-D

Comments are closed.