0

I need to redirect only http://example.org/ to http://www.something-other.org/ in Apache 2.4.

Just the homepage. Nothing else. I.e. http://example.org/?page=blog or http://example.org/contact.html should NOT redirect.

All I could find was redirecting a page completely to another site like:

Redirect "/" "http://www.something-other.org/"

This is a bit better:

RewriteEngine On
RewriteRule ^/$ http:// www.something-other.org/ [L,R=301]

It redirects only the homepage, but also, if there is a query string.

rubo77
  • 2,282
  • 3
  • 32
  • 63
  • 1
    [Server Fault is a site for information technology professionals](http://serverfault.com/help/on-topic) -- as such we have certain professional expectations when people ask a question here, and one of those expectations is that your question shows you did some **research**, found and **read the vendor documentation** and/or **tried a solution** before asking the internet for help. You're not expected to solve everything yourself, but then at least you can [write a great question](http://meta.serverfault.com/a/3609/37681) that we can help you answer. – HBruijn Jan 06 '17 at 09:07
  • I stated the question accordingly to http://serverfault.com/questions/491007/ so I thought this would be fine – rubo77 Jan 06 '17 at 09:09
  • That Q&A isn't very stellar either but might have been better received because 4 years ago nginx was not nearly as popular (or well documented) as today, and even more surprisingly that already has a answer for Apache as well... (thank you for the edit BTW) – HBruijn Jan 06 '17 at 09:15
  • @rubo77 Why would you even care about the query string? – kubanczyk Jan 07 '17 at 00:58
  • Why the downvotes? What could be enhanced in this question? – rubo77 Jan 08 '17 at 08:59
  • 2
    The solution of "this question already has an answer" points to a page that is essentially like reading a whole book about redirects. That is the problem with this voodoo and that is why people would like to ask one simple specific case question. Pointing them to a book type of FAQ is no answer. The real answer is very short and it is a time saver to find only that answer for the specific question of redirecting only / – labradort Sep 13 '18 at 12:36

1 Answers1

5

You need to use mod_rewrite:

RewriteEngine On
RewriteCond "%{QUERY_STRING}" "^$"
RewriteRule "^/$" http://something-other.org/ [L,R=301,QSD]

The QSD flag discards the query string.

Enable mod_rewrite and restart apache:

root@host:~# a2enmod rewrite
root@host:~# service apache2 restart
rda
  • 1,887
  • 1
  • 12
  • 20
  • But I found out something a bit better: `^/$` does only redirect the homepage, but also if there is a querystring. How can I not redirect, if there is a querystring? – rubo77 Jan 07 '17 at 00:49
  • @rubo77 this should do it. – rda Jan 07 '17 at 01:34
  • QSD seems redundant to me, since this rule only matches if there is no query string at all, due to its RewriteCond – lucaferrario May 24 '18 at 15:22