Forum Moderators: phranque

Message Too Old, No Replies

Help with Mod_Rewrite Redirect w/ Query String

mod_rewrite, Query Strings

         

meltzman12

8:18 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Hi,

I've got a situation where I need to do a redirect in .htaccess including query strings. I've tried several things, but with no luck. In short, this is what I'm trying to accomplish:

http://www.example.com/gallery/index.php?pageId=0&start=0

needs to redirect to:

http://www.example.com/gallery/index.php?pageId=117&start=0

The only difference is the pageID=117 is changing, but different content gets populated into index.php based on pageId.

I tried this, but it doesn't do anything:

RewriteEngine On
RewriteRule ^gallery/index?pageId=0&start=0$ gallery/index.php?pageId=117&start=0

I'd love some help here, as I'm quite baffled.

Thanks,
Matt

jdMorgan

8:27 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Matt,

Welcome to WebmasterWorld!

This recent thread [webmasterworld.com] should answer your question and help get you started.

Jim

meltzman12

8:43 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



jdMorgan,

Thank you for pointing me to that thread.

I've tried the following, but to no avail:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^pageId=([0-9]{3})$
RewriteRule ^index\.php$ http://www.example.com/gallery/index.php?pageId=%1&start=0 [R=301,L]

But it doesn't seem to be working (I'm very new to this). I changed the 2 to a 3 in the RewriteCond because three digits are needed for the new page (117), but I also don't see where in the above mod_rewrite, the 117 should be identified.

I then tried this, but still no luck:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^pageId=117$
RewriteRule ^index\.php?pageId=0&start=0$ http://www.example.com/gallery/index.php?pageId=%1&start=0 [R=301,L]

I also noticed, that the "&start=0" is not a vital part of the string, and can be eliminated from both URLS.

jdMorgan

9:03 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be careful with your regular-expressions pattern-anchoring. Your current query string pattern will not match if there is anything except the PageId=<number> in the query string; because you have included both a start-anchor and an end-anchor, only an exact match on PageID=<3-digit-number> will be accepted.

It's not clear what the interactions are between the two query parameters. For example, what if the "start=" value is not zero -- do you still want to do the rewrite in that case, or not? Does the "start=" value need to be passed to the new URL? Are there always two parameters, or could there be more or less? Are you expecting to rewrite multiple values of PageId, or just one? If more than one, how do the old PageIds map to the new ones?

Clearly, all we can do is point you in the right direction, as it would take you more time to type in *all* of the necessary details about your URL architecture than it would take to become very familiar with mod_rewrite.

For more information, see the documents cited in our forum charter [webmasterworld.com], especially the regular-expressions info, and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

meltzman12

9:11 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



I understand what your saying, but I think what I'm trying to do should be quite simple (I hope).

index.php displays different content based on what the "pageId" is equal to. Whenever the the browser is pointed to:

http://www.example.com/gallery/index.php?pageId=0

I want it to automatically display the content from pageId=117. I figured the best (and maybe only way since I can't do a meta refresh in the code itself) is to do this with a mod_rewrite.

pageId=43, or pageId=68, or pageId=3, etc. all display unique content. The only instance I'm looking for is to have pageId=0 display (or redirect to) pageId=117. All others should stay the same.

As I appended to one of my previous posts, the &startId=0 can be removed from the equation entirely.

-Matt

jdMorgan

10:55 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
# If index.php page 0 requested (Note: Match only page 0. Do not match page 01, 02, etc.)
RewriteCond %{QUERY_STRING} pageId=0[^0-9]*
# redirect to page 117
RewriteRule ^index\.php$ http://www.example.com/gallery/index.php?pageId=117 [R=301,L]

Jim