Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many RewriteBase in one .htaccess file?

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?

like image 838
Martti Laine Avatar asked Mar 12 '10 18:03

Martti Laine


People also ask

What is RewriteBase in htaccess?

What is RewriteBase. RewriteBase directive allows you to easily set the beginning of all relative paths used in . htaccess file. Its value is used in the target/destination path mentioned in rewrite rules in . htaccess file.

What is RewriteBase Apache?

RewriteBase allows you to adjust the path that mod_rewrite automatically prefixes to the result of a RewriteRule . A rewrite within the context of . htaccess is done relative to the directory containing that . htaccess file. The immediate result of the RewriteRule is still relative to the directory containing the .

What does l mean in htaccess?

L (last - stop processing rules) Last rule: instructs the server to stop rewriting after the preceding directive is processed. N (next - continue processing rules) NC (case insensitive) NE (do not escape special URL characters in output) NS (ignore this rule if the request is a subrequest)

What is $1 in htaccess?

In your substitution string, $1 contains the contents of the first set of parens ( hello ), while $2 contains the contents of the second set ( there ). There will always be exactly as many "dollar" values available in your substitution string as there are sets of capturing parentheses in your regex.


2 Answers

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]
like image 136
Gumbo Avatar answered Oct 01 '22 13:10

Gumbo


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.

like image 35
cytsunny Avatar answered Oct 01 '22 14:10

cytsunny