Click to See Complete Forum and Search --> : Mod Rewrite query string - condition not recognized
bunner bob
09-25-2008, 12:51 PM
I'm attempting to redirect "old style" query string URLs to a new URL structure using Mod Rewrite. For example, redirect:
the-store.php?categoryId=23&parentId=2
to:
store/2/23/
But the Mod Rewrite rule I'm using is having trouble matching the input URL.
Here's an example rule:
RewriteRule ^the-store.php?categoryId=([0-9]+)&parentId=([0-9]+)$ http://www.mydomain.com/store/$2/$1/ [L]
I get nothing. Tried removing the $ at the end of the condition. In fact, it only works if I delete everything after the question mark - e.g. this works:
RewriteRule ^the-store.php? http://www.mydomain.com/store/ [L]
So it seems that the query string is breaking the condition.
I've also tried various combinations of [R], [L] and [R,L] to no avail.
My hair is turning gray - can anyone help?
- Bob
bunner bob
10-01-2008, 08:11 PM
Well glad to know at least I'm not an idiot - it really IS hard to figure out :)
Shorts
10-01-2008, 09:19 PM
you're putting it in the wrong direction :]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/([0-9]+)/([0-9]+)$ /the-store.php?categoryId=$1&parentId=$2
RewriteRule ^store/([0-9]+)/([0-9]+)/$ /the-store.php?categoryId=$1&parentId=$2
bunner bob
10-01-2008, 11:17 PM
you're putting it in the wrong direction :]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/([0-9]+)/([0-9]+)$ /the-store.php?categoryId=$1&parentId=$2
RewriteRule ^store/([0-9]+)/([0-9]+)/$ /the-store.php?categoryId=$1&parentId=$2
No, I already have that taken care of. What I want to do is capture all the requests for the old-style URLs (with php query string) and do a permanent redirect to the new style URLs. This so the search engines stop indexing the old URLs, since they don't match our sitemap.
Shorts
10-02-2008, 01:39 AM
No, I already have that taken care of. What I want to do is capture all the requests for the old-style URLs (with php query string) and do a permanent redirect to the new style URLs. This so the search engines stop indexing the old URLs, since they don't match our sitemap.
Ah, my bad was confused and that makes more sense now. Maybe something like this then:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{query_string} ^categoryId=([0-9]+)&parentId=([0-9]+)$
RewriteRule ^the-store.php$ http://www.mydomain.com/store/$2/$1 [L]
bunner bob
10-02-2008, 09:40 PM
Getting closer - that does redirect me, but the redirected URL is:
http://www.mydomain.com/store///?categoryId=36&parentId=2
So 2 problems:
1. The captured numbers from the condition aren't showing up in the rewrite.
2. The query string is still stuck on the end. I've read about this latter problem and can probably track down the answer again.
A little research got me to the right answer. First, to use backreferences from a preceding RewriteCond, you have to use %1 instead of $1. Second, to get rid of the query string on the redirect I just need to add a ? at the end of the destination URL. So here's the final working result (including a permanent redirect for Google, etc.):
RewriteCond %{query_string} ^categoryId=([0-9]+)&parentId=([0-9]+)$
RewriteRule ^the-store.php$ http://www.mydomain.com/store/%2/%1/? [R=permanent,L]
Thanks loads for getting me on the right track!
- Bob
Shorts
10-03-2008, 01:40 AM
Awesome glad you got it working.