[wp-hackers] How to make root relative urls work in subdomain

Marcus Pope Marcus.Pope at springbox.com
Mon Oct 31 04:48:24 UTC 2011

Hey Ryann, I would suggest that you always use /css/base.css. Then on your dev and staging site I would use an .htaccess rewrite rule that checks for the project name at the start of the request_uri. If it's not there rewrite the url with it prepended at the start. Since .htaccess files have access to environment variables you could even make it controlled by code in the wp_config.php file so you wouldn't have to create a new custom .htaccess file for every project. 

Alternatively you could add a custom rewrite rule in wordpress itself. The wordpress rewrite api http://codex.wordpress.org/Rewrite_API is fully capable of handling this scenario as well, though performance would be faster in mod_rewrite (.htaccess). This api is how wordpress maps urls like /year/month/day to the correct file, as you probably already know there is no actual file for such a url. Considering this is the premise for 90% of the urls processed by the core it is still suitable for your situation. And for dev environments the performance loss is probably not noticeable. 

That way when you switch to production everything will work without the specialized rules and you will have zero performance loss along with zero refactoring costs. 

Hope that helps, if you have questions about specifics just ask!

-marcus pope
(sent from my iphone)

On Oct 30, 2011, at 9:03 PM, "Ryann Micua" <ryannmicua at gmail.com> wrote:

> I know there is this absolute-vs-relative url debate going on here and 
> I've learned quite a bit about the subject and more importantly, about 
> the community.
> Anyway, this is my problem: I have a testing server, 2 actually - 
> http://wp.dev/ (localhost) and http://dev.example.com/ to host my 
> project sites
> when working on client projects I create a new subdirectory under the 
> above domains so that the root to the new development site becomes 
> http://wp.dev/projectname/ and http://dev.example.com/projectname/ and 
> install wordpress from there. And for people who still don't get what 
> I'm trying to do here, to view a page, I access it now through 
> http://wp.dev/projectname/about-page/
> Now, to link to my assets (css, js, images) I currently do <?php echo 
> get_template_directory_uri(); ?>/css/base.css or something like that 
> which prints out an absolute url.  Now I have no problem with that or 
> with doing that for my wp projects.
> My issue is when I have to create non-wp sites like say static pages. 
> for linking to assets, I have to spell out the whole url or at least do 
> something like
> /projectname/css/style.css then, when I need to upload it to the final 
> server, i have to change the urls now to /css/style.css
> So my question, what do i need to do so that in my pages, all I need to 
> write for my url is /css/style.css and it'll work if I put that in 
> http://wp.dev/projectname/ and http://dev.example.com/projectname/ and 
> on http://www.projectname.com/ or basically no matter where i put it.
> -- 
> *Ryann Micua*
> /Web Developer/
> ------------------------------------------------------------------------
> Website: /www.pogidude.com/
> Skype: /rmicua/
> Mobile: /+639169273059/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list