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.

6
  • 1
    Server Fault is a site for information technology professionals -- 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 that we can help you answer.
    – HBruijn
    Jan 6, 2017 at 9:07
  • I stated the question accordingly to serverfault.com/questions/491007 so I thought this would be fine
    – rubo77
    Jan 6, 2017 at 9: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 6, 2017 at 9:15
  • @rubo77 Why would you even care about the query string?
    – kubanczyk
    Jan 7, 2017 at 0:58
  • 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, 2018 at 12:36

1 Answer 1

6

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
3
  • 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 7, 2017 at 0:49
  • @rubo77 this should do it.
    – rda
    Jan 7, 2017 at 1:34
  • QSD seems redundant to me, since this rule only matches if there is no query string at all, due to its RewriteCond May 24, 2018 at 15:22

Not the answer you're looking for? Browse other questions tagged .