17

I have a domain and a wordpress-blog on same server. Now I have a problem (surprise). The wordpress is located on /httpdocs/blog/ and domain is pointing to /httpdocs/ and I'm trying to redirect it to /httpdocs/domain/. But, obvisiously, I have permalinks in Wordpress.

Here's my current .htaccess:

RewriteEngine On

RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]

But as you already propably assumed, this doesn't work. Wordpress' permalinks affects to /domain/ also, so my images and other urls go wrong.

Any advice? Is it possible to use RewriteBase like this?

Martti Laine
  • 12,655
  • 22
  • 68
  • 102

2 Answers2

13

No, you can only have one base URL. Just rewrite your rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]

RewriteCond %{HTTP_HOST} =example.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 2
    It seems the last `RewriteBase` directive wins for the entire .htaccess file. – MrWhite Aug 14 '13 at 19:30
  • 1
    In fact, it seems the initial idea behind the use of `RewriteBase` in the question is wrong anyway. You would have needed to write the `RewriteRule` pattern as `^blog/.` anyway, regardless of the value of `RewriteBase`. `RewriteBase` does not affect the URI matched against the _pattern_, it only affects relative _substitutions_. – MrWhite Aug 14 '13 at 20:12
2

I come to this post when I am trying to find solution for a similar problem. It seems that there can be more then one base URL, but the logic does not stop after rewrite. If the URL hit both rewrite base, all the rewrite will be run. Therefore, the strictest rewrite base should be put at the end of the file. For this example, it should be:

RewriteEngine On

RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]

RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

Noticed that as both rewrite are being run, so if the rewrite contradicts, you will need to fall back to the accepted answer.

cytsunny
  • 4,838
  • 15
  • 62
  • 129