2

I want to redirect all requests on my old domain to the homepage (root site) of my new domain. I thought it would work with a:

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

(see: .htaccess redirect all pages to new domain) but for a strange reason, when I try for example to access the URL www.olddomain.com/faq/pdf.php?cat=7&id=93&artlang=de I get redirected to www.newdomain.com/?cat=7&id=93&artlang=de. What could be the problem?

In the past I had some problems because this domain is in a subdirectory (as addon-domain) of my new main-domain, but in the .htaccess of the main-domain I've got a:

RewriteCond %{HTTP_HOST} ^([^.]+\.)*olddomain\.com
RewriteRule .* - [L]

Which disable this kind of errors. Any other ideas? I'm trying this now since hours.

EDIT: I've googled when I got that the problem is the query string.

Community
  • 1
  • 1
Andy
  • 129
  • 1
  • 2
  • 14

4 Answers4

3

If you want to redirect to your new domain without path and query string, you must append an empty query string

RewriteEngine On
RewriteRule .* http://newdomain.com/? [R=301,L]
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • This is exactly what I want, but it doesn't work and still passes the query string. I know it's strange but I can't get the source for the problem. – Andy Jan 13 '13 at 21:40
  • @Andy Please verify, that you have a final `?` at the target URL. I just tested this in my environment and it works with the final `?`. – Olaf Dietsche Jan 13 '13 at 21:45
  • I've got a "/?" at the end of my url. But it still keeps the query. Is it possible that it's an apache bug as mentioned in my linked question or doesn't this have anything to do with it? I've used also Saluman's variant. – Andy Jan 13 '13 at 22:13
  • @Andy Salman's is the same as mine. And as I said, I tested this with my apache 2.2 in a virtual host .htaccess and it works. There must be something different in your environment, which is causing this. Maybe, you use this in a directory context or in the main config or something else. – Olaf Dietsche Jan 13 '13 at 22:35
  • Sorry my fault, it was a problem with the browser cache. You've got the right solution. Thanks a lot, you've solved my problem! :) – Andy Jan 13 '13 at 22:37
2

mod_rewrite appends query string to the rewritten URL if no query string was specified. Add an empty query string to avoid the problem (adding a ? is sufficient):

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/? [R=301,L]

Also read about the QSA flag.

Salman A
  • 262,204
  • 82
  • 430
  • 521
1

UPDATED:

RewriteEngine On
RewriteCond %{HTTP_HOST} (?:www\.)?oldomain\.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/?  [R=301,L]

Updated as an example, not as an answer.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • Thanks, but my target is to NOT pass the rest of the URL. My problem is it gets passed automatically. – Andy Jan 13 '13 at 21:38
  • Okay. Got it backwards. Try adding the `DPI` flag. Like this [R=301,L,DPI] and see what happens. – Felipe Alameda A Jan 13 '13 at 21:41
  • @Salman Thanks a lot. I just copy-paste the rule in the question and did not notice the `L` was missing. – Felipe Alameda A Jan 13 '13 at 21:42
  • Thanks for your help, but it still keeps the query string. Used your latest suggestion. – Andy Jan 13 '13 at 22:09
  • You are welcome. But, as I said, I miss understood your question based on the 2 URLs in your example. The last one is missing part of the URI and I thought that was the problem. However, now I understand what you want and the solution is in Olaf's and Salman's answers. That's the way to remove the query string. Make sure you test it in a real server. Most testing servers don't support the `?` to remove the query. – Felipe Alameda A Jan 13 '13 at 22:15
  • I tested it at my productive environment, seems to be that it doesn't support the "?". I've never heared about this. Is there a workaround to 301 redirect without query string? – Andy Jan 13 '13 at 22:18
  • I don't think it is a server problem. Maybe the incoming URL is not matching the rule conditions and the rule is always skipped. So, there is no query removal. Try adding a condition to your rule to make sure the incoming URL matches the old domain. – Felipe Alameda A Jan 13 '13 at 22:33
  • My fault, (Chrome) browser cache problem. The ? solved the problem. – Andy Jan 13 '13 at 22:38
0

use rewrite to redirect from an old domain to a new domain that includes the full path and query string:

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]