WordPress: How to change folder, URL or all media links at once?

Problem

I’ve just wanted to change several things on my WordPress site:

  1. URL location
    from iknowsomething.com/blog
    to simple iknowsomething.com
  2. WordPress folder location
    from main-server-folder (=domain folder)/blog
    to main-server-folder/iknowsomething (=domain folder)
  3. Permalinks
    from iknowsomething.com/index.php/year/monthnum/day/post-title
    to simple iknowsomething.com/post-title

By this operation following problems come across:

  • How to change WordPress folder and/or URL to preserve old links working from the Internet?
  • How to change all WordPress media links at once to avoid changing hundreds of them manually?

Solution

I don’t want here to write about how to move WordPress site, because this can be done going through the codex.wordpress.org Moving WordPress manual. But all problems described above you cannot find in this manual except one, that you have to change all media links manually 🙂 or semimanual like this, loosing old links validity. I didn’t want that nor mundane work, so all I had to do was to change original WordPress .htaccess mod_rewrite procedure (see Using Permalinks) with two new regular expressions (coloured orange) as follows :

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /


# Added: Rewrite old post links to be reached from the Internet
RewriteRule ^blog/index\.php/[0-9]{4}/[0-9]{1,2}/(.*) index.\php/$1 [R=301,L]

# Input example:
http://iknowsomething.com/blog/index.php/2011/04/how-to-fix-bricked-wd-my-book-world-edition-or-install-brand-new-disk/
# Result:
http://iknowsomething.com/how-to-fix-bricked-wd-my-book-world-edition-or-install-brand-new-disk/

# Added: Rewrite old links including internal media links
RewriteRule ^blog/(.*) $1 [R=301,L]
# Input example:
http://iknowsomething.com/blog/wp-content/uploads/2011/06/fittext.png
# Result:
http://iknowsomething.com/wp-content/uploads/2011/06/fittext.png

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

Save this code (properly modified to your needs) as .htaccess to WordPress folder with index.php. Now all old Internet links is working and you have to change no single one media link 😉

That’s all

If you want to play with mod_rewrite and pretty permalinks on your own, refer to this:

O mod_rewrite po polsku na home.pl (podręcznik)webhost.pl, php.rk.edu.pl lub seoparty.net.

Discussion

  1. Ria Karrels

    Wow, incredible blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is magnificent, as well as the content!. Thanks For Your article about 301 Moved .

Give your feedback