Welcome to WebmasterWorld Guest from 34.231.247.139

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

.htaccess redirecting files with query strings?

     
2:13 pm on Apr 20, 2006 (gmt 0)

New User

10+ Year Member

joined:July 22, 2002
posts:8
votes: 0


Hi,

I have a site which is totally changing in structure and will no longer use query strings. However I want to redirect these pages with parameters to an equivalent page with a permanent 301.

However if I use the following line of code in my .htaccess file it will append the query string;
redirect 301 /example.php [newexample.com...]

eg. If I try to access the following page;
http://www.example.com/example.php?parameter=123

It will redirect you to [newexample.com...]

It is carrying over the query string when redirecting i.e.?parameter=123

However I donít want the redirect to pass on the query string, how do I stop them from being appended to the new page?

And is there a way to specify a filename with a parameter which I can then redirect?

Im sure i could do this with a regular expression but im not quite sure how.

Thanks

3:36 pm on Apr 20, 2006 (gmt 0)

New User

10+ Year Member

joined:July 22, 2002
posts:8
votes: 0


Think ive solved it using;

RewriteEngine on
RewriteRule ^test/test2.php$ http://www.example.com? [R=301,L]

12:36 am on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


The second rule and the techniques discussed in this post [webmasterworld.com] might be useful to you.

Jim

9:06 am on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 7, 2003
posts:1230
votes: 0


Think ive solved it using;

RewriteEngine on
RewriteRule ^test/test2.php$ http://www.example.com? [R=301,L]

that is the exact way to replace the querystring. per default RewriteRule will use the original querystring until you define your own: it's a this-or-that situation.

only if you use the [QSA] (= Query String Append) parameter, you can mix both.

11:15 am on May 4, 2006 (gmt 0)

New User

10+ Year Member

joined:July 22, 2002
posts:8
votes: 0


Cheers guys
1:08 pm on May 4, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 17, 2003
posts:18
votes: 0


I'm not sure if my problem applies to what you were doing.....but I don't necessarily understand the solution so I'm going to ask my guestion. Forgive me if I should have put this in a new thread if this is something different, let me know and I'll start a new thread. Otherwise hopefully we can sole it here.

I have asked this question before......and the answer for a universal mod rewrite I guess created an endless loop or whatever and it wouldn't work, unless the rule was just wrong.

But I have about 7000 products, hence 7000 urls that I was rewriting a certain way and then changed the method of rewrite to include the item name in the url and shorten the url so it didn't appear that the page was deep in several directories. So I began rewiting them a different way. It now looks like they are top level pages. The jest of this is I'm trying to 301 redirect all of them to their new style.

domain/cp-app/sports-memorabilia/ItemID is now

domain/sports-memorabilia-Item-Name--ItemId.html

I made a 301 rewrite rule for each and every one to redirect them, and "I think" it worked, but that meant putting 7000 or so lines in my htaccess file and it ballooned to 1.5 meg in size and slowed my dynamic site to a crawl. I quickly abandoned that method.

Does anyone know of a rewrite or redirect method that would work? I read briefly here on webmasterworld in another thread something about a script or external from the htaccess file to accomplish this but didn't understand it at all.

The bottom line is I am trying to redirect 301 a url that has been written one way to rewriting it another way. If anyone has methods or suggestions I would really really appreciate their help. If it involves a coding or script or something I am willing to talk on the phone and/or to pay someone for their time.

I know I am not the first person who has ever changed the method of rewrite they are using for alot of urls. There must be a method that can solve this issue. I hope someone out there has the knowledge and experience to put me on the right track.

Thanks in advance for your time and help!

3:05 pm on May 4, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


A common solution is to use mod_rewrite's RewriteMap directive.

RewriteMap can be used to call a script (PERL is used most often) to do a database lookup to translate one URL to another. This is only needed in cases such as yours, where the old URL does not contain all of the information needed to generate the new URl by simple test rearrangement or substitution.

RewriteMaps can be used in .htaccess, but they must be defined at the server configuration level; As such, this solution is not available to sites on name-based or IP-based virtual servers. Usually, you'll need to be hosted on a VPS or a dedicated server to have this level of access.

In that case, you can still use mod_rewrite to rewrite all or most requests to a script, which can then either include and output the desired page content, or generate an external redirect to the correct URL. The first option is 'silent' while the second is visible to the user.

Jim

3:15 pm on May 4, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 17, 2003
posts:18
votes: 0


I knew there was a method, somehow.......

I am on a vps.

But I have no clue how to execute or understand this method.

<snip>

[edited by: jdMorgan at 11:29 pm (utc) on May 4, 2006]
[edit reason] TOS #20. [/edit]

11:30 pm on May 4, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


If you have database and PERL skills, then this shouldn't be too hard. RewriteMaps are easy -- the second time you implement one. ;)

Jim

2:24 pm on May 5, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 17, 2003
posts:18
votes: 0


Honestly, I'm really a novice. I have minor rewrite knowledge, beyond that I'm a site owner that needs help from others.

Ideas?

4:12 pm on May 6, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 17, 2003
posts:18
votes: 0


Old rule and url

RewriteRule ^cp-app/prod/sports-memorabilia/([^/]*)/?$ shop/cgi-bin/cp-app.cgi?pg=ste_prod&sports-memorabilia&ref=$1 [L]

/cp-app/ste_prod/sports-memorabia/HM-PHOTO323K

cp-app is the cgi script, prod is the page (short for product), sports-memorabilia is just a fake dir to put those keywords in the url, ref is the item ID
---------------------------------------------------------------------
New rule and url

RewriteRule ^sports-memorabilia-(.*)\.html shop/cgi-bin/cp-app.cgi?seo=item--$1 [L]

/sports-memorabilia-Jason-Varitek-Gold-Coin-Photomint--HM-PHOTO323K.html

Top level page now, sports-memorabilia is again just fake keywords inserted, Jason Varitek Gold Coin Photomint is the items name or <Title> tag, HM-PHOTO323K is the itemID, and of course it now has html added to the end
--------------------------------------------------

There is a routine in the shopping cart cgi script as you see that is being called in the second rule that pulls the correct information to create the url.

Is there some way to redirect the first rule to the already existing script using the item id and have it create the corresponding new style url?

Is that along the same track of what you suggested doing?

5:38 pm on May 6, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


If the values of 'ref' in the old URLs are entirely equivalent to those of 'id' in the new URLs, then you can do this. Otherwise, there is no way for mod_rewrite to 'associate' the new 'id' value with the old 'ref' value -- It does not know how to translate from ref to id.

So if ref and id are not the same thing, you have to use an existing or new database to do the translation. And that's where RewriteMap and a database-access script come in.

Jim

5:47 pm on May 6, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 17, 2003
posts:18
votes: 0


HM-PHOTO323K is the ref, item id, id, whatever you want to call it in both cases. That is the product id that never changes no matter what in either case. It is a common value in both methods and is what is actually used by the cgi script to call the right item.

You can see in the second method that there is a routine in the cgi script that is being called that is what forumlates the new style url somehow based on the item id it is looking for.

If there is a method to somehow redirect to it, is that what you are thinking?

6:10 pm on May 6, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Again, if ref == id, then concatenating the two rules should work:

RewriteRule ^cp-app/prod/sports-memorabilia/([^/]+)/?$ /shop/cgi-bin/cp-app.cgi?seo=item--$1 [L]

This extracts the old 'ref' substring from the old URL, and rewrites to the cp-app script using that extracted value as the value for 'seo=itm---'
Jim
6:43 pm on May 6, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 17, 2003
posts:18
votes: 0


Unfortunately it isn't working. It is taking me to a page that it goes to when it doesn't know what an item id is.

I placed this rule ahead of the first rule and then also remarked out the first rule.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members