0

The first requirement was to redirect traffic from http://www.old-domain.org to https://example.press -- but only do so for the root of the domain; any specific page should be available on the old domain.

So I did a little studying, and this did the job:

RedirectMatch "^/$" "https://example.press"

(interestingly enough, https://htaccess.madewithlove.be/ claimed it doesn't work...)

Then, a next request came in: also redirect another URL, of a form:

http://www.old-domain.example/site/index.php?act=news

, but in such a way that if any specific news story was requested would not get redirected (e.g., http://www.old-domain.org/site/index.php?act=news&id=12345)

This proved very challenging. Many docs and articles mentioned this is not possible with Redirect/RedirectMatch directives, but possible with RewriteCond + RewriteRule, which I gathered should look something like this:

<IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond %{QUERY_STRING} act=news
          RewriteRule ^(.*)$ https://example.press [R=302,L]
</IfModule>

(again, https://htaccess.madewithlove.be/ claims it works)

However, nothing seems to be working. No redirects are happening. Any glaringly obvious misconceptions on my part, eh?

alexakarpov
  • 103
  • 5
  • In what _context_ are these directives? ie. in `.htaccess` or directly in your server config? "(interestingly enough, ... claimed it doesn't...)" - If I try that tool, it claims that it does? "nothing seems to be working. No redirects are happening." - not even your original `RedirectMatch` redirect? Remove the `` container (it's not required), do you get an error? – MrWhite Sep 04 '18 at 09:03
  • 1
    yes, these are placed in .htaccess – alexakarpov Sep 04 '18 at 14:26

1 Answers1

1

I believe this will work:

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{QUERY_STRING} act=news RewriteCond %{QUERY_STRING} !act=news&id=12345 RewriteRule ^(.*)$ https://example.press/ [R=302,L] RewriteRule ^/$ https://example.press/ [R=302,L] </IfModule>

There are two rules, and the first rule has two conditions. The second conditional on the first rule uses the ! operator to negate the match on act=news&id=12345. There is an implicit "AND" between the conditionals on the first rule, so they both need to be true.

The second rule then matches the / page only, with no conditions.

guzzijason
  • 1,410
  • 8
  • 18
  • thank you! looks promising, will try once I get home, and will let you know how it worked out – alexakarpov Sep 04 '18 at 14:27
  • Since the "other URL" is of the form `/site/index.php?act=news` then you should presumably also include the URL-path in the `RewriteRule` _pattern_. Currently, it will redirect every URL-path and only check the query string. Also, since this is in `.htaccess` you'll need to remove the slash in the 2nd `RewriteRule` _pattern_. You could simplify the 2nd condition and just check against `!id=12345`, since you already know that `act=news` is present in the query string by the 1st condition and this would also mean the URL params can be in any order. – MrWhite Sep 04 '18 at 23:55
  • OK, this did work! Well at first it didn't, but I figured that .htaccess was littered with several other directives, which were written by someone I don't know at some point in 2005 (the last time I know the old creators of the original website did their dark magic). So decided to go bold and remove those. If it breaks anything for anyone, they'll let me know =D – alexakarpov Sep 05 '18 at 00:31
  • 1
    That's why I happen to hate .htaccess files - they can come in handy in a pinch, but you are likely to trip over them when you least expect it! – guzzijason Sep 05 '18 at 01:09
  • and here's another observation - while the browser indeed gets redirected to a new domain, it receives the same query string attached to it - even though the new domain runs a different stack and new routes and has no use for that querystring. – alexakarpov Sep 05 '18 at 01:54
  • @guzzijason I think I agree - it makes much more sense to throw these directives inside the respective virtualservers, imho – alexakarpov Sep 05 '18 at 01:56
  • If you want, you can add the `QSD` flag to the rule, which will cause it to drop the query string, such as `[R=302,L,QSD]` – guzzijason Sep 05 '18 at 02:21