Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

My current .htaccess file looks like this (autogenerated by WP)

$ cat .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/healthydebate/index.php [L]
</IfModule>

This is the standard WordPress rewrite setup. The issue I'm having is setting up site relative links within my pages (eg, a footer link pointing to terms-and-conditions.php). When I'm on the home page (ie: blog/index.html) the link works. When I'm on a sub page (ie: blog/categoryname/article-title) the link fails because, being a relative link, it is relative to blog/categoryname (ie: blog/categoryname/terms-and-conditions.php).

Before someone suggests "just use an absolute URL", this is not an option (or a best practice, really) because we are developing on a different domain and server structure than the final deployment server, so all absolute URLs would need to be rewritten every time we deploy.

To make the issue a little more complicated, we must also be root-directory-path agnostic. That is to say that even though the blog currently resides at /blog/clientsite/ it may eventually be moved to some other directory, for example /clientsite/ or even /STAGING/blogs/clientsite/ so RewriteBase may be a little tricky.

I'm sure the solution is dead simple, but mod_rewrite is definitely not my strong suit.

share|improve this question
    
Best practice would be to have your development and production environments be identical in every way that matters, no? –  jgoldschrafe Dec 17 '10 at 18:56
    
Yes, yes it would. In an ideal environment. Although this is off-topic, consider the following scenario: client has their own webserver, the site will be published to their webserver's htdocs (root); we have a DEV box on which multiple sites in development are hosted behind our firewall, this site will be developed at blogs/clientname; we have TEST box on an external server, available to the client in a pw-protected directory, testing site is at STAGING/clientname/blog. It's not ideal for developers or migration, but it's ideal for the customer and for security. –  Tom Auger Dec 18 '10 at 21:50

2 Answers 2

up vote 0 down vote accepted

You definitely want to be constructing absolute URLs within your footer template, but you don't want to be hardcoding them. You probably want the WordPress function get_bloginfo('wpurl') to get the base URL of your WordPress site as it's specified in your application config, and then stack your relative URL on top of that. It's a straightforward enough function call, but here's the doc anyway:

http://codex.wordpress.org/Function_Reference/get_bloginfo

share|improve this answer
    
Excellent info. Can you explain why I would want to be using absolute URLs rather than relative ones? –  Tom Auger Dec 18 '10 at 21:38
    
Because with mod_rewrite and SEO-friendly URLs, you don't know where you are in your directory hierarchy until you start un-mangling your URLs. Might as well just avoid mangling in the first place. –  jgoldschrafe Dec 19 '10 at 18:14
    
sigh it's true. I was limited by the Theme, so I rewrote one of its widgets to include get_bloginfo('wpurl') or its twisted echo cousin bloginfo('wpurl'). Certainly simplifies things a log. –  Tom Auger Dec 20 '10 at 18:23

You need to add RewriteBase /blog

and remove the 'blog/' portion from the RewriteRule

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /healthydebate/index.php [L]
</IfModule>

When you move the directory in the future, just update the RewriteBase.

share|improve this answer
    
Thanks for this. Is there any way to avoid having to rewrite anything? My migration protocol is already pretty lengthy and I would love to avoid adding any extra steps. –  Tom Auger Dec 18 '10 at 21:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.