Score:0

Is it possible to set up .htaccess rule for incoming traffic from 301 redirect?

mg flag

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

HBruijn avatar
in flag
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 avatar
in flag
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?
kz flag
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.
deeholzman avatar
mg flag
Sorry about the confusion, it's an Apache server - it's Siteground hosting which uses both Apache & nginx but ours is Apache.
deeholzman avatar
mg flag
@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 avatar
mg flag
@MrWhite - Normal visitors to the new site won't request this URL - it's only a URL (page on the new site) that redirected users from the old site will visit. First, we want to detect visitors redirected from the old site, and then redirect those visitors to our PHP page (on the new site) which contains a script that will consult a cross reference table in our database that contains all of the old and new Post IDs. It will then find the new Post ID and use a PHP redirect to forward them to that new Post ID.
deeholzman avatar
mg flag
To clarify, my main question is if it's possible to obtain a referrer and a querystring from a 301 redirect. If we can get those things, we'll be able to use our .htaccess rules to direct that traffic (and only that traffic) to a PHP page on our site that redirects people where we want them based on the querystring.
Score:0
cu flag

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]
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.