Welcome to WebmasterWorld Guest from 54.225.38.176

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 Redirect for individual URLs using query strings

getting rid of query strings in base URIs

     
6:35 pm on Sep 20, 2013 (gmt 0)

New User

5+ Year Member

joined:Oct 7, 2008
posts:11
votes: 0


So this post was too old to add to: [webmasterworld.com...]

I'm trying to redirect
/123/456/?postname=example

to
/890/example/


This is to be implemented on a WP site with only the standard WP stuff in .htaccess currently. I also need to implement this for example2, example3, etc.

Haven't seen anything that addresses this on an URL by URL basis, so any help would be much appreciated.

Thanks in advance!

Tim
7:44 pm on Sept 20, 2013 (gmt 0)

New User

joined:Jan 10, 2013
posts: 15
votes: 0


Hey Tim

You should be able to do a redirect of the URL and query using:

Redirect 301 "/123/456/?postname=example" http://www.yourwebsite.com/890/example/ 


Or you can use a redirect match to redirect all URL in a folder such as:

RedirectMatch 301 ^/123/456/(.*)$ http://www.yourwebsite.com/890/example/$1
8:25 pm on Sept 20, 2013 (gmt 0)

New User

5+ Year Member

joined:Oct 7, 2008
posts:11
votes: 0


Thanks Choc, but that's where I started . . . with no success. I think the standard "Redirect 301" line is hiccuping on the query string. And I would like to do this one URL at a time, for some other reasons, so the second option would be a last resort.
8:32 pm on Sept 20, 2013 (gmt 0)

New User

joined:Jan 10, 2013
posts: 15
votes: 0


Hmm, that can be frustrating because it shouldn't. You did include the "" (quotations) in your attempts right?
8:39 pm on Sept 20, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
posts:1097
votes: 0


Mod_Alias only "sees" the URL-path, not the query_string. To match the query_string you'll need to use Mod_Rewrite with a RewriteRule [to match the URL-path] and RewriteCond [to match the query_string].

There are some tutorials in the Library [webmasterworld.com] that should explain some things better than I have time for right now -- I recommend starting with Beginning Mod_Rewrite + Mod_Rewrite and Regular Expressions near the bottom of the page.
9:00 pm on Sept 20, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10551
votes: 10


extracting "example" from the query string of the requested url to insert into the substitution string is the easy part.
how are you going to generate the "890"?
9:19 pm on Sept 20, 2013 (gmt 0)

New User

5+ Year Member

joined:Oct 7, 2008
posts:11
votes: 0


Thanks all. I will try some of these things and report back.
9:43 pm on Sept 20, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13066
votes: 308


If you have WordPress, your htaccess already uses mod_rewrite. That means you have to use mod_rewrite for all redirects, including conditionless path-only ones that could otherwise be done in mod_alias (Redirect by that name). In fact, if you've got any redirects using mod_alias, translate them to mod_rewrite format.

Edit:
Is it always /890/ ? If not, you have to either detour to a php script or-- if only a few pages are involved-- code separate explicit redirects for each.
9:54 am on Sept 21, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


The first question to ask is how many URLs are you wanting to redirect. Is it only a few or is it hundreds or thousands?

The next question is are all the URLs that will be redirected already created? That is, do you want to redirect only URLs that already exist, or will this rule need to cater for additional URLs in the future?

Is the "890" part common to all of the new URLs, or does it vary depending on what is in the URL request that you want to redirect?

Is the /123/456/ part constant in all the requested URLs, or does it also vary?
6:37 pm on Sept 25, 2013 (gmt 0)

New User

5+ Year Member

joined:Oct 7, 2008
posts:11
votes: 0


Lucy - it is always /890/
g1 - It's only a couple dozen URLs. All URLs are already created, just need a one time fix. 890 is common. 123/456 is also constant.
9:06 pm on Sept 25, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


No need for complicated stuff here. You'll just need one RewriteRule for each affected URL.

Each Rule will also need a preceding RewriteCond looking at THE_REQUEST to validate the requested query string.

If the part of the old URL that varies is the same string as used in the part of the new URL that varies, you might be able to use a single RewriteCond/RewriteRule pair for the whole lot.

[edited by: g1smd at 9:09 pm (utc) on Sep 25, 2013]

9:07 pm on Sept 25, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13066
votes: 308


OK, now let's lay out in English what the pattern is. Is there any relationship between old URL and new URL? "A couple dozen" is borderline: If you had to do them all manually it could be done in htaccess without unreasonably slowing the server, but it would also be worth the trouble of putting together a php page.

/123/456/?postname=example
to
/890/example/


Always 890. Good. But what about the 123/456 part? Will there also be requests in the form
234/567/?postname=example
that you DON'T want to redirect?

In general you need to show at least two examples before you can pick out the pattern and devise a rule.

:: vague mental association with longago JIR article about single-trial learning in the domestic darning needle ::