Problem detecting empty REQUEST_URI with Apache mod_rewrite

I start Apache with a redirect rule as follows:

RewriteCond %{HTTP_HOST} ^1st-domain\.com RewriteRule ^(.*)$ http://2nd-domain.com$1 [R=permanent,L] 

This successfully redirects http://1st-domain.com to http://2nd-domain.com However, when REQUEST_URI is empty, I want to redirect to a third domain.

 RewriteCond %{HTTP_HOST} ^1st-domain\.com$ RewriteCond %{QUERY_STRING} ^$ RewriteRule ^(.*)$ http://3rd-domain.com$1 [R=permanent,L] 

But this does not work and instead redirects to 2nd-domain.com

My rules are organized as follows:

 RewriteCond %{HTTP_HOST} ^1st-domain\.com$ RewriteCond %{QUERY_STRING} ^$ RewriteRule ^(.*)$ http://3rd-domain.com$1 [R=permanent,L] RewriteCond %{HTTP_HOST} ^1st-domain\.com RewriteRule ^(.*)$ http://2nd-domain.com$1 [R=permanent,L] 

Any suggestions? Thank you in advance.

UPDATE

The first rule should direct empty request_uri to 3rd-domain.com, the second rule should direct non-empty request_uri to 2nd-domain.com

USEFUL INFORMATION You can enable mod_rewrite debugging using this snippet:

 <IfModule mod_rewrite.c> RewriteLog "/home/domain.com/logs/rewrite.log" RewriteLogLevel 3 </IfModule> 

A very useful debugging option that I did not know about.

+6
source share
8 answers

This should work:

 RewriteCond %{HTTP_HOST} ^1st-domain\.com RewriteRule ^$ http://3rd-domain.com [R=permanent,L] RewriteCond %{HTTP_HOST} ^1st-domain\.com RewriteRule ^(.+)$ http://2nd-domain.com/$1 [R=permanent,L] 

Hope this helps!

Note: REQUEST_URI is slightly different between httpd.conf and .htaccess, it starts with an extra backslash in httpd.conf. This means that in httpd.conf, the first rewrite rule should be ^\/$ , not just ^$ .

+9
source

I use the following to catch an empty REQUEST_URL:

RewriteEngine on

RewriteCond %{REQUEST_URI} "^/$"

RewriteRule ^(.*) http://%{HTTP_HOST}/my/another/url

+8
source

This should work:

 RewriteCond %{HTTP_HOST} ^1st-domain\.com RewriteRule ^(.*)$ http://2nd-domain.com$1 [R=permanent,L] RedirectMatch ^/$ http://3rd-domain.com 
+1
source

Your rules redirect the request with an empty QUERY_STRING .

For an empty request_uri you can use

 RewriteCond %{HTTP_HOST} ^1st-domain\.com$ RewriteRule ^$ http://3rd-domain.com$1 [R=permanent,L] RewriteCond %{HTTP_HOST} ^1st-domain\.com RewriteRule ^(.*)$ http://2nd-domain.com$1 [R=permanent,L] 

The first rule will first match <empty> , and then the tests for <non-empty or empty> (which cannot be <empty> now, since we processed it before)

0
source

In multisites, such a redirect works for me for empty requests:

 RewriteCond %{HTTP_HOST} ^1st-domain\.com$ RewriteCond %{REQUEST_URI} "^/$" RewriteRule ^$ http://3rd-domain.com/ [R=permanent,L] 
0
source

If the request is empty, apache redirects to index.html, so -RewriteCond% {REQUEST_URI} index- can help you.

0
source

I tried the options shown on this page, all I wanted to do was check if REQUEST_URI is empty (or in this particular case, / ):

 # Check of the request uri is just 1 char long (checking for a slash was harder): RewriteCond %{REQUEST_URI} ^.$ 
0
source

This worked for me:

 RewriteCond %{HTTP_HOST} ^(www\.)?1st-domain\.com$ RewriteCond %{REQUEST_URI} ^/$ RewriteRule .* http://3rd-domain.com/ [L,R=permanent] RewriteCond %{HTTP_HOST} ^.*$ RewriteRule .* http://2nd-domain.com%{REQUEST_URI} [L,R=permanent] 

without quotes when empty checking

0
source

Source: https://habr.com/ru/post/886038/


All Articles