Problem detecting empty REQUEST_URI with Apache mod_rewrite

♀尐吖头ヾ 提交于 2019-12-18 16:52:07

问题


I am running Apache witha redirect rule like this:

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 the 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 ordered like this:

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

  1. Empty REQUEST_URI: http:/1st-domain.com
  2. Non-empty REQUEST_URI: http://1st-domain.com/something

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

USEFUL TIDBIT You can turn on mod_rewrite debug with this snippet:

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

Very useful debug option I hadn't known.


回答1:


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 it 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 ^$.




回答2:


I am using the following to catch empty REQUEST_URL:

RewriteEngine on

RewriteCond %{REQUEST_URI} "^/$"

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




回答3:


This should work:

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



回答4:


Your rules redirects request with empty QUERY_STRING.

For 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>, then tests for <non-empty or empty> (which can't be <empty> now since we've processed it before)




回答5:


On multisites such redirection works for me for empty requests :

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



回答6:


If request is empty apache 'redirects' to index.html so -RewriteCond %{REQUEST_URI} index- might help you.




回答7:


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

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



回答8:


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]

whitout the quotes on the empty validation



来源:https://stackoverflow.com/questions/5684940/problem-detecting-empty-request-uri-with-apache-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!