Welcome to WebmasterWorld Guest from 184.73.126.70

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)

5+ Year Member



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 Sep 20, 2013 (gmt 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 Sep 20, 2013 (gmt 0)

5+ Year Member



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 Sep 20, 2013 (gmt 0)



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

WebmasterWorld Senior Member Top Contributors Of The Month



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 Sep 20, 2013 (gmt 0)

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



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 Sep 20, 2013 (gmt 0)

5+ Year Member



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

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



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 Sep 21, 2013 (gmt 0)

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



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 Sep 25, 2013 (gmt 0)

5+ Year Member



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 Sep 25, 2013 (gmt 0)

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



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 Sep 25, 2013 (gmt 0)

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



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 ::
 

Featured Threads

Hot Threads This Week

Hot Threads This Month