Forum Moderators: phranque

Message Too Old, No Replies

Redirect Query String URL to new URL only, Keep Query String

         

blakemiller

2:19 pm on Nov 17, 2014 (gmt 0)

10+ Year Member



I'm trying to redirect a php page with a query string to simply a new domain.tld url while keeping the original query string in the url. The page is the same, the Querty string is the same. Only thing different is the host website URL. Here is the setup:

http://example.org/podcast.php?pageID=23 > http://example.net/podcast.php?pageID=23

I've tried a few variations of the redirects such as:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^podcast\.php$ http://example.net/podcast.php [R=301,L]
and/or
RewriteRule ^(.*)$ http://example.net/podcast.php [R=301,L]

But can't get any of them to work. Would love some insight to learn more about advanced .htaccess commands and usage. (I've got a bunch of regular page redirects in place which work well.)

[edited by: Ocean10000 at 5:39 pm (utc) on Nov 17, 2014]
[edit reason] Examplified. [/edit]

phranque

4:51 am on Jan 1, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, blakemiller!


http://example.org/podcast.php?pageID=23 > http://example.net/podcast.php?pageID=23


RewriteCond %{QUERY_STRING} ^id=([0-9]*)$

^id is not going to match pageID=23

blakemiller

11:49 pm on Feb 7, 2015 (gmt 0)

10+ Year Member



Thank you for responding. I realize that the command will not work. Do you know what will work to do what I am trying to accomplish?

lucy24

6:53 am on Feb 8, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Real parameter:
pageID
Parameter in rule as currently written:
id
See the difference?

The second problem is that the form
^pageID=\d+$

will only work if "pageID" is your only parameter. If there are potentially others, the condition would have to say
(^|&)pageID=\d+

though you can probably simplify to
\bpageID\b

where \b means any word boundary (including both the non-word character & and the beginning/end of any string). In fact, even the \b is superfluous unless you've also got parameters named, say, "rampageID" that you need to exclude.

Is the object here to redirect only pages with this specific parameter, and not pages that don't have it? Does the parameter have to have a particular value, or is it enough for it to exist? Does it have to have some value? The following forms all do slightly different things. They may or may not make a difference, depending on what happens on the originating site (the one that sets the parameter):

RewriteCond %{QUERY_STRING} pageID=\d
(parameter exists, and has a positive numerical value)
RewriteCond %{QUERY_STRING} pageID=
(parameter exists, and has been defined)
RewriteCond %{QUERY_STRING} pageID
(parameter exists)

If you were redirecting all pages "podcast.php" regardless of query string, then you wouldn't need a RewriteCond at all. By default, query strings just go along for the ride.

blakemiller

12:11 pm on Feb 8, 2015 (gmt 0)

10+ Year Member



Thanks lucy24. I am somewhat understanding what your saying but my lack of knowledge in this area is evident. In this particular example/post, I have only a single, fixed string and it's fixed. I don't have other parameters, pages, values, etc.

Again, I just need to redirect this one, exact string as I wrote in the OP. It's a single URL only. (I'm trying to keep an iTunes podcast feed alive. We moved our site to a new domain name, and iTunes requires a redirect in place from the original site to keep the original feed alive. It's been broken for a while now.)

phranque

8:23 pm on Feb 8, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



in that case you were very close the first time you tried:

RewriteEngine On 
RewriteCond %{QUERY_STRING} ^pageID=23$
RewriteRule ^podcast\.php$ http://example.net/podcast.php [R=301,L]