Israel Cruz

A developer's blog

Wordpress in shared hosting

We've been there at some point; for some reason, you have to use Wordpress for something that is not a blog (of course it was not on you), so you start making custom plugins, shaping up somebody's pet shop theme with custom functions and filters, and after a few tests and proper changes on wp-config.php (where you think is the place to put things like domain name, so WP knows how to render links and other stuff) so you proceed to upload everything to your server… and nothing works.

Back in the day, I had to figure it out myself and took me more than an hour, this is what I had to do, and hopefully it helps.

Assuming you are working in your local machine, in a subfolder like localhost/yourlocalwp, and want to upload your files to a production server in a shared hosting (as most WP sites) like yourdomain.com, just follow these steps:

1 - Back up your DB

  $ mysqldump -u root -p my_local_database -r db.dump

2 - Change WP default domain for yours in db.dump

For some (very stupid) reason, WP automatically stores absolute links to your site posts, and virtually everywhere, so you just have to change every occurence of http://localhost to https://yourdomain.com

  $ sed 's/http:\/\/localhost\/yourlocalwp/https:\/\/yourdomain.com/g' 
  db.dump > remote_db.dump

3 - Import db in your remote server

Use phpMyadmin or ssh:

  $ mysql -uroot -p --default-character-set=utf8 my_remote_database
  $ SOURCE remote_db.dump

4 - Update wp-config.php accordingly

/** The name of the database for WordPress */
define('DB_NAME', 'my_remote_database');
/** MySQL database username */
define('DB_USER', 'my_remote_user');
/** MySQL database password */
define('DB_PASSWORD', 'my_remotepa55');
/** MySQL hostname */
define('DB_HOST', 'my_remote_host');

5 - Upload your files (using ssh + git or your fav ftp)

If you upload all your files it should work fine in your remote server, except for one thing; if you are using "pretty URLs" (that is what WP calls them) as in myproductiondomain.com/my-post-name you’ll get a 404 error when attempting to visit any page other than homepage, to fix it follow this final step:

6 - Update your `.htaccess`

From this

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

To this

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

These two lines are used by Apache’s mod_rewrite to deal with custom URLs.


And thats it