Redirecting a query string in Apache

Joined
Dec 28, 2020
Messages
56
Likes
97
Degree
0
Hey guys. I want to redirect each link with a query string to a specific address by appending the string. I have the following in my htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} catid=([0-9]+) [NC]
RewriteRule (.*) catid%1? [R=301,L]

When a user hits domain.com/?catid=10, they are successfully redirected to domain.com/catid10, which is what I want.

However, when they go a directory deeper (domain.com/category/?=catid10), they are not redirected to domain.com/category/catid10.

I have been reading manuals but can't find the answer. Please help?
 
I don’t know all the regex off the top of my head and I’m on mobile, but it appears you’re capturing the query string (which is good) but you aren’t using it in rewrite rule. Is it defaulting to the root domain + catid even at deeper folder levels? If so, I imagine this is the solution.
 
I don’t know all the regex off the top of my head and I’m on mobile, but it appears you’re capturing the query string (which is good) but you aren’t using it in rewrite rule. Is it defaulting to the root domain + catid even at deeper folder levels? If so, I imagine this is the solution.
No, at deeper levels the redirect is not triggered. It just opens the directory. If the user is accessing domain.com/category/?=catid10, the address still contains the query string, but what they see is the domain.com/category/ page.
 
Fixed this now:

RewriteCond %{QUERY_STRING} (?:^|&)catid=(\d+) [NC] RewriteRule (.*?)/?$ $1/catid%1 [QSD,R=301,L]
 
Back