27

I need some help with URL rewriting. The case is similar with this one.

I have a working Zend Framework site. Now I must add a blog in Wordpress (also working). I've chosen not to indulge in ZF controller-/action-/route-making; I've seen a couple of tutorials about this and I consider them too much for a "plain" redirection. Now, about that "redirection"...

This is how it should look like:

  • www.site.com (points to /var/www/zf)
  • www.site.com/blog (points to /var/www/wp)

I know that I should stop www.site.com/blog to enter ZF's innards and I'm currently doing this with RewriteRule ^blog - [NC,L] in its .htaccess, but that's about it. As @jason said, "just pass it through to Wordpress", but I don't know how exactly to do that.

Related question:
I never tried it, but does Apache support this in two different vhosts?
ServerName www.site.com (vhost for ZF site)
ServerName www.site.com/blog (vhost for WP site)

Community
  • 1
  • 1
nevvermind
  • 3,302
  • 1
  • 36
  • 45
  • Try adding the following to `.htaccess` in the parent directory above the directory of interest: `RedirectMatch ^/foo/$ /foo/bar/` or `RedirectMatch ^/foo/$ /bar/baz/`. Also see [How to get apache2 to redirect to a subdirectory](http://serverfault.com/q/9992/145545). – jww Nov 06 '16 at 08:21

2 Answers2

60

www.site.com (points to /var/www/zf)

www.site.com/blog (points to /var/www/wp)

The easiset way to achive this, where you want a sub-url to point outside the VirtualHost's DocumentRoot directory, is to create an Alias...

Inside the VirtualHost block add:

Alias /blog /var/www/wp

<Directory /var/www/wp>
    Options All
    AllowOverride All
    order allow,deny
    allow from all
</Directory>

*This assumes you have PHP enabled in some way for that directory.

rightstuff
  • 6,412
  • 31
  • 20
  • I've tested this locally and it works, but wp links point to (say) `http://localhost/wordpress/?page_id=7` (which works), instead of `http://local-play/blog/?page_id=7`. – nevvermind Jun 11 '11 at 07:10
  • Cancel my previous comment. It's because I installed WP *before* this new configuration. When installing as "/blog" sub-url, it works fine. Thanks, @rightstuff. – nevvermind Jun 11 '11 at 07:24
  • 2
    This requires that Apache mod_alias is enabled. http://httpd.apache.org/docs/2.2/mod/mod_alias.html – Laizer Apr 30 '14 at 10:40
  • I'm trying this with a laravel project and it works for the top-level alias (i.e. www.site.com/blog) but not for alias sub-directories (i.e. www.site.com/blog/abc) – mils Mar 09 '17 at 02:28
  • Cancel my previous comment too, I managed to get it to work for sub-directories by adding this line to my Laravel project's .htaccess file "RewriteBase /blog" – mils Mar 09 '17 at 02:32
0

The ServerName trick will not work: you can not have path names in a ServerName directive.

For mod_rewrite you should be able to get away with something like this:

RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*)$ wp/$1 [L]
RewriteRule ^(.*)$ zf/$1 [L]
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Ah, no: you'll need to put that in the top level folder. If you put it in the ZF folder's .htaccess you won't be able to affect directories ABOVE zf (such as your blog folder). – Femi Jun 10 '11 at 14:10