homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

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)

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

I'm trying to redirect

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!




 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)

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)

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)

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)

Thanks all. I will try some of these things and report back.


 9:43 pm on Sep 20, 2013 (gmt 0)

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.

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)

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)

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)

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)

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.


Always 890. Good. But what about the 123/456 part? Will there also be requests in the form
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 ::

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved