Darius
Darius

Reputation: 1664

Htaccess rewrite GET request to another GET request and another with 2 GET variables

I have two url's I'm trying to rewrite, for the past... 4-5 hours (headache now).

I am trying to rewrite

/arts/tag/?tag=keyword 

to

/search/art?keywords=keyword

Looking at other questions I formulated my rewrite like this

RewriteRule /arts/tag/?tag=([^&]+) search/art?keywords=$1 [L,R=301,NC] 

and

RewriteRule ^arts/tag/?tag=$ /search/art\?keywords=%1? [L,R=301,NC]

I tried with backslashes and without, no luck.

Also tried

RewriteCond  %{QUERY_STRING} /arts/tag/?tag=([^&]+) [NC]
RewriteRule .* /search/art\?keywords=%1? [L,R=301,NC]

The second one is similar,

/arts/category?id=1&sortby=views&featured=1

to

/art/moved?id=1&rearrange=view

The reason I change the get variable name is for my own learning purpose as I haven't found any tutorials for my purpose. I also changed category to moved since the categories have changed and I have to internally redirect some ID #'s.

RewriteCond %{QUERY_STRING} id=([^&]+) [NC] // I need the path in there though, not just query string, since I'll be redirecting /blogs/category and /art/category to different places.
RewriteRule .* /art/moved/id=%1? [L,R=301,NC]

Any help will be appreciated. Thank you.

Upvotes: 0

Views: 769

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

Assuming the queries in the original URLs have nothing in common with those in the substitution URLs, maybe this will do what you want, using the first keyin the query as a condition and to identify the incoming URL:

RewriteEngine On
RewriteBase /
# First case
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING}  \btag\b
RewriteRule .* http://example.com/search/art?keywords=keyword?  [L]

Will map this: http://example.com/arts/tag/?tag=keyword

To this: http://example.com/search/art?keywords=keyword

# Second case
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING}  \bid\b
RewriteRule .* http://example.com/art/moved?id=1&rearrange=view?  [L]

Will map this: http://example.com/arts/category?id=1&sortby=views&featured=1

To this: http://example.com/art/moved?id=1&rearrange=view

Both are mapped silently. If the new URL is to be shown in the browser's address bar modify the flags like this [R,L]. Replace R with R=301 for a permanent redirect.

Upvotes: 1

Related Questions