Redirecting a query string in Apache

Joined
Dec 28, 2020
Messages
46
Likes
80
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?
 

Ryuzaki

お前はもう死んでいる
Moderator
BuSo Pro
Digital Strategist
Joined
Sep 3, 2014
Messages
5,129
Likes
9,819
Degree
9
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.
 
Joined
Dec 28, 2020
Messages
46
Likes
80
Degree
0
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.