Forum Moderators: phranque

Message Too Old, No Replies

re-writing a URL

         

infinitiguy

2:07 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



I have a bunch of url's to re-write that contain ?'s in them, and I'm not sure exactly the syntax to use. The url is www.domain.com/app/forum.jspa?category=119 and I need to rewrite that to www.newdomain/app2/newcategoryname.

Is there a way to make the ? in the rewrite rule, be taken literally?

jdMorgan

2:25 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not part of a URL, but rather, data attached to a URL to be passed to the resource (script) *at* that URL. Therefore, the Apache Redirect, RedirectMatch, and RewriteRule directives cannot 'see' the query string when examining (only) the URL.

So, to test or create back-references to query_strings, use mod_rewrite's RewriteCond directive testing the %{QUERY_STRING} variable, in addition to a RewriteRule pattern matching the URL(s) to which that query is attached.

Jim

g1smd

2:35 pm on Mar 23, 2009 (gmt 0)

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



*** The url is www.example.com/app/forum.jspa?category=119 and I need to rewrite that to www.newexample.com/app2/newcategoryname. ***

Your description of a rewrite is exactly backwards. You need a redirect for that action, and then a rewrite in the other direction. Try this way of thinking about things:

You need to link to the new URLs from the pages of your site. It is these links that 'define' URLs.

You then need a rewrite to take that new format URL request, and fetch the content from your pre-exisiting internal dynamic server filepath. The rewrite will not contain a domain name and will end with [L].

You'll also need a redirect to take a direct client request for the old URLs (with parameters included), and redirect to the new URL for that content. That redirect will contain the full domain name in the target URL, and end with [R=301,L].

List them in this order: redirects first, rewrites last.

infinitiguy

3:31 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



yes, you're right. re-write was the wrong word :)
I think what I'm trying to achieve is confused now.

I have an old jive integrated forum site. I'm replacing it with a new clearspace site. So... if someone has favorited www.example.com/app/forum.jspa?category=119 I want that request to get forwarded to www.newexample.com/app2/newcategoryname. I don't need to do any URL cleanup, or masking, or anything. Just fwd requests from old, to new and have the URL change in the window. No proxies, no anything fancy :)
I had something like this, but it didn't work the way I expected
RewriteRule http://www.example.com/library/kbcategory.jspa?categoryID=660 [newexample.com...] [R=301,L]

What I ended up getting was redirected to [newexample.com...]

I suspect this is because of the ?, but I could be wrong.

g1smd

3:35 pm on Mar 23, 2009 (gmt 0)

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



RewriteRule cannot see domain names or query strings.

You need a RewriteCond for each of HOST_NAME and QUERY_STRING if you need to look at those.

A redirect will contain the full domain name in the target URL, and end with [R=301,L].

You can clear the query string from being re-appended to the target URL by placing a question mark on the end of the target URL.

From your description, you need a redirect from old URL to new URL. It doesn't look like you need any sort of rewrite as you have moved to a new system.

infinitiguy

4:09 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



So do I need something like...
RewriteCond %{QUERY_STRING} categoryID=660
RewriteRule ^(.*) [newexample.com...] [R=301,L]

I'm not too familiar with RewriteConditions, so forgive me if I've completely butchered the concept :) I am using RewriteRule because that's what we've used for all of our redirects, even to other sites(or at least that's what others have used).

infinitiguy

4:39 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



i figured the syntax out
RewriteCond %{QUERY_STRING} categoryID=660
RewriteRule ^/(.*) [newexample.com...] [R=301,L]

So I need a pair of these for everything I'm going to do. Correct?

jdMorgan

5:06 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you would define "everything I'm going to do" for us, that would be helpful. You may actually be able to do this with just a few rules -- or maybe even just one.

What old URLs+querystrings need to be redirected?
What URLs+querystrings might look like those, but should not be redirected?
What are the defining characteristics that distiguish these two sets of URLs+querystrings?

For example, if *all* URLs requested with *any* query string containing "categoryID=" should be redirected, then simply removing "660" from your current rewritecond pattern would do that.

Jim

g1smd

5:24 pm on Mar 23, 2009 (gmt 0)

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



You don't need that leading slash in the Rule pattern if this is going into .htaccess.

You do need it if this rule is going into the httpd.conf file.

infinitiguy

5:24 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



nope. I have 50 different categories, that will redirect to the same(but different url syntax) category on the new system, so I need to individually define each category, with a matching url. It would be lovely if I was doing say a bunch of categories to 1 url... but that's not the case unfortunately.

So for example, ID=22 redirects to /blah/blah2/blah3 but ID=23 might redirect to /blah/page/index.html and ID=24 redirects to /welcome.html

I was able to do some clever find/replaces and generate the 100 or so lines I needed to accomplish this, and implemented on my test server and all seems to be working as expected.

g1smd

5:27 pm on Mar 23, 2009 (gmt 0)

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



When you redirect to an index page, don't include the "index.html" bit in the target URL.

End the URL with a trailing slash, and let your DirectoryIndex directive find and serve up the correct content for this.

Make sure you force the www in each redirect, so that you don't create a non-www vs. www duplicate content issue on the new site.

jdMorgan

5:51 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, since you're doing ad-hoc URL replacement, then you can't do them all with one simple rule.

However, since you've got config-level access to this server, you might want to look into defining and using a RewriteMap... Much easier to maintain than 50 discrete rules... :)

Jim

infinitiguy

12:51 am on Mar 24, 2009 (gmt 0)

10+ Year Member



Ah, did a quick google. Rewrite map is kind of like another config include where all the rewrites for this are just stored in a text file, correct?

btw, thanks for everyone's replies. Couldn't have done it without you!

g1smd

1:00 am on Mar 24, 2009 (gmt 0)

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



The idea here is to nudge you in the right direction so you can fix your own code.

Glad the strategy worked in this case. RewriteMap will be quite useful to you. :)