We have a permanent 301 redirect that is set up for an old site that is now being redirected to a page on a new site. Is it even possible to set up an .htaccess rule on our new sites Apache server to direct requests coming in from this old '301 redirected' site to a specific page on the new site?

We have a rule in our .htaccess file that contains a condition that the request is coming from the old domain and if it contains a querystring in the requested URL. Depending on the querystring, we're redirecting to a certain page, but it doesn't seem to be working so I'm wondering if it's even possible:

RewriteCond %{HTTP_REFERER} ^http://oldomain.com [NC]
RewriteCond %{QUERY_STRING} ^thequerystring=([0-9]*) 
RewriteRule ^(.*)$ https://www.newdomain.org/redirect.php?theimage=%1 [R=301,L]

The Apache version is Apache/2.4.57 (Unix) and the nginx is 1.22.0

  • mod_rewrite rules and .htaccess are typical Apache httpd tools but you then mention a nginx version. Ngninx does not support .htaccess files. Typically people only have one web server running at the same time so what are you running?
    – HBruijn
    Jun 16, 2023 at 7:39
  • Your problems reads like: the redirect from the old site is not pointing to the desired page on the new site. Rather than doing the obvious, change where the old site redirects to, on the new site you want to identify requests that are the result from that redirect and then redirect them a second time?
    – HBruijn
    Jun 16, 2023 at 7:42
  • What is the "page" (ie. URL) that users are redirected to on the new site? The rule you've posted is seemingly trying to match every URL-path? The regex ^thequerystring=([0-9]*) matches thequerystring=<anything> (and the backreference can be empty) - is that the intention? Can normal visitors to the new site request this URL? Or is it only a URL that redirected users from the old site will visit? Otherwise, there is no way to detect a "redirected" request, since the redirect itself does not generate a referer.
    – MrWhite
    Jun 16, 2023 at 14:03
  • Sorry about the confusion, it's an Apache server - it's Siteground hosting which uses both Apache & nginx but ours is Apache.
    – deeholzman
    Jun 16, 2023 at 14:10
  • @HBruijn - Yes, We do want to identify requests that are the result from that redirect. These requests coming from the old site will have a querystring that contains a Post ID. We want to redirect those requests (that contain the old Post ID) to a PHP page on the new site that finds a corresponding new Post ID in our database. This PHP page will look up the old Post ID in a cross reference table, find the new corresponding Post ID and then direct visitors to it.
    – deeholzman
    Jun 16, 2023 at 14:15

1 Answer 1


If both sites are on the same server:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^thequerystring=([0-9]*)$
RewriteRule ^(.*)$ https://www.newdomain.org/redirect.php?theimage=%1 [R=301,L]

You must log in to answer this question.

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