Forum Moderators: phranque

Message Too Old, No Replies

Need help with 301 wildcards

I need to rewrite 301s usign wildcards

         

dougwo

1:06 am on Aug 4, 2008 (gmt 0)

10+ Year Member



I have a site where for some reason, Google keeps getting URLS that look like this:

http://example.com?fid=nnn

In that file, I have a re-direct that sends the user to:

http:/example.com/show/nnn

And this works. But Google still tells me that I have over 4000 invalid URLS because apparently their spider doesn't pick up the redirect. (I'm doing php headers command, not meta redirect).

I'd like a 301 command in my htaccess that would work for any value of nnn something like this:

redirect 301 http://example.com?fid=%1 [mysite.com...]

but I am unsure of all that is required to parse off the nnn value and assign it to %1. Can anyone help?

[edited by: jdMorgan at 1:34 am (utc) on Aug. 4, 2008]
[edit reason] example.com [/edit]

jdMorgan

1:37 am on Aug 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to use mod_rewrite [httpd.apache.org], and specifically with a RewriteCond to examine and create a back-reference to the query string ( the server variable name is %{QUERY_STRING} ). See the resources listed in our forum Charter, and try a search for previous threads on this subject for more info.

[added] You may want to review this previous thread: Changing Dynamic URLs to Static URLs [webmasterworld.com], if that is what your overall goal is here. [/added]

Jim

[edited by: jdMorgan at 1:40 am (utc) on Aug. 4, 2008]

dougwo

1:30 pm on Aug 4, 2008 (gmt 0)

10+ Year Member



Thanks Jim...

But I'm having a difficult time with my specific example. I have many places where the string ?fid=nnn exists. Any time that string exists in the URL, I want the entire new url to be:

http://example.com/show/nnn

This is to take care of may old links in Google where no matter what page I was one before you could pass an fid. BTW, nnn is always numeric.

I am having difficulty with the specific regex chars and syntax for my example. All of the examples I find are for turning a single dynamic page into a static one.

jdMorgan

3:56 pm on Aug 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both the mod_rewrite documentation and the thread cited in the links above above contain examples of matching variable strings using regular expressions. The cited thread contains a specific example of rewriting URLs with appended query-strings back to a static form.

Due to the number of requests and the limited number of contributors here, we are set up to answer specific questions and focus on helping members learn, rather than just dashing off code. The concepts you need are:

  • Testing and parsing query strings requires the use of RewriteCond in mod_rewrite.
  • A regular-expressions pattern is used to match all or part of the requested URL and/or query string's fixed or variable characters
  • Enclosing all or part of the regular-expressions pattern in parentheses creates a "back-reference."
  • A back reference is a variable containing the characters that matched the regex pattern.
  • Back-references to pattern-matches in a RewriteCond are named %1 through %9, inclusive.
  • Back-references to pattern-matches in a RewriteRule are named $1 through $9, inclusive.
  • These back-references may be included in the substitution (destination) URL or filepath in the RewriteRule.

    The linked citations above plus the documents cited in our Forum Charter will get you well on your way to solving this problem. And more to the point, reading those documents will give you the background to understand any solution we might help you develop here, so that you can maintain and modify it as needed -- and perhaps use it as a basis to understand further applications of the compact and powerful mod_rewrite tool.

    Jim

  • dougwo

    6:00 am on Aug 5, 2008 (gmt 0)

    10+ Year Member



    Thanks Jim. However, I am the CEO of the company and am not blessed with the time to become an Apache expert. I just want this problem solved. I'll look elsewhere. Thanks!