Welcome to WebmasterWorld Guest from 54.224.103.186

Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

How to deal with URLs when deploying a Wordpress site

     
11:38 pm on Jul 9, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


One incredibly annoying aspect of Wordpress is that the site root URL is set in the database.

Normally, I would
- create a local site
- commit to git
- push the code to the repo
- pull down to the server

The site config should be in code and the content in the database, but in Wordpress it's mixed pele-mele, so you can't maintain a simple workflow, because every time you pull your live data down, your site breaks. If you forget and push your dev data up, your site breaks.

The solution is simple:

- make sure your wp-config.php is in your .gitignore so it's not tracked by version control. That means you can have a different one on your dev and live servers

- in your wp-config.php file, add two lines to override whatever the siteurl and home page urls are in the DB

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');


- now you can develop locally and push to your test and live platforms normally without borking your site and without having to manually edit your database every time you pull down your live data.

[codex.wordpress.org...]
12:40 am on July 10, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 0


Nice tip ergo!
2:08 pm on July 15, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4393
votes: 2


Thanks,
Exactly the kind of tip that makes you wonder "Why I did not think about it?" :)
1:59 am on July 22, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


Turn out this method and the related ones have their limits. Some plugins seem to tap into the system and call the site_url() function before the override settings take effect (I would think nothing could happen before wp-config.php fires) and pull the value from the DB.

I've always thought this was one of the stupidest "features" of Wordpress... arrgghh
12:01 pm on July 22, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 0


I think the WordPress dev team would welcome your thoughts if you were so inclined.
7:24 pm on July 22, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


Where does one submit that sort of thing to Wordpress?

I'm curious what their logic is on this. In any case, I set up wp-config.php as follows

- remove all DB connection info
- have three include statements that use absolute paths that are environment specific in the form

if (file_exists('/path/to/subdomain/on/test/machine/config-test.php')) {
include('/path/to/subdomain/on/test/machine/config-test.php');
}
elseif (file_exists('/path/to/docroot/on/live/machine/config-live.php')) {
include('/path/to/docroot/on/live/machine/config-live.php');
}
elseif etc...

Then each file has site-specific info like the DB connection and

define('WP_HOME','http://test1.example.com');
define('WP_SITEURL','http://test1.example.com');


Aside from a few plugins it works fine. Everything I've worked with except WP uses root-relative URLs, so it's easy to put it in version control and run a dev/test/live workflow.

What is the expected workflow with Wordpress? Do people just do everything on their live sites? If not, how do other people handle the URL problem?
7:32 pm on July 22, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4393
votes: 2


@ergophobe You could have two similar WP on the same server. One your main root site and another one set in another dir.
9:37 pm on July 22, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 0


[make.wordpress.org...]

You'll need a forum membership but then you can submit a ticket and or get involved with building out the core. You can also join in on the IRC chats. Otherwise, just engage @nacin on Twitter.
11:19 pm on July 22, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


So of course the absolute vs root relative question has been discussed ad nauseum in the WP community issue queue

e.g. [core.trac.wordpress.org...]

It will probably never be accepted, but there is a plugin to make WP use root relative URLs if you so choose.
[wordpress.org...]

@henry0 - Are you referring to my file_exists() calls?

The actual implementation would be up to the developer to make sure that there is no ambiguity or you would never have multiple hits. Some situations will be harder than others.
11:56 pm on July 22, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4393
votes: 2


@ergophobe
It regards: Do people just do everything on their live sites?
4:22 pm on July 23, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


Got it. In that case my critique holds. Doesn't matter how similar your server is. If it doesn't have the same URL, then you have to muck with the Wordpress database manually every time you import your production DB to your development platform. Makes no sense.
7:57 pm on July 23, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4393
votes: 2


Point well taken, thanks
7:51 pm on July 25, 2014 (gmt 0)

New User

joined:July 25, 2014
posts: 2
votes: 0


my simplistic approach is to develop the site in a sub folder on the server while using a password protect plugin. Then I move the wordpress files to the root and use a search and replace plugin to revise the url site wide. I also use a maintenance page plugin that supports html content to shut it down for a few minutes to make sure the theme hasn't been trashed. Usually it goes pretty well and takes about 5 minutes tops. I run the search and replace plugin several times during the process to make sure all instances of the subfolder url are changed.
4:24 pm on July 26, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


Hi Maggymae. Thanks for joining in.

That's similar to what I used to do, minus the plugins. The root problem for me is that all such processes are prone to error and can bring a live site down unnecessarily.

Ideally, I should be able to take my exact live data, inject it into my test platform, and if it passes all automated tests (see [webmasterworld.com...] I should be able to just git push and git pull without going into maintainence mode or anything like that.

In any case, there shouldn't be steps in the process that, if I forget them (search and replace all instances of the URL in the DB, which is a crazy requirement from a CMS), bring the system down.
11:43 am on July 27, 2014 (gmt 0)

New User

joined:July 25, 2014
posts: 2
votes: 0


:) gotcha... I haven't seen a site go down as of yet... but some themes have gotten a bit mangled.
6:08 pm on July 27, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


The "root relative" plugin I referenced above helps too. Now I can move the database willy nilly without worrying about taking sites down.
7:03 pm on Sept 14, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


Okay, scratch that. Wordpress still has serious migration problems. I think you have to run your dev site in a virtual machine that has the same file strucutre as your target machine and which has your main domain resolve to localhost.
3:18 am on Sept 15, 2014 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8214
votes: 121


Actually, it works fine for WP core. The problem is themes and plugins that use absolute file paths and URLs that mess everything up.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members