Reputation: 2012
I have the following rewrite in my .htaccess file, it is still landing on a 404 instead of redirecting.
RewriteCond %{QUERY_STRING} tab=auto_data(.*)$
RewriteRule ^(.*)$ https://test.example.com/automobile-data/ [L,R=301]
There are multiple pages that can possibly have the tab=auto_data
query string parameter, and there are quite possibly other QSP appended behind tab=auto_data
as well.
I need to redirect any URL that contains the QSP of tab=auto_data
to a new page in the site. The domain would remain the same, just the page name is changing.
What am I doing wrong here?
Upvotes: 1
Views: 59
Reputation: 45988
The only other directives are the standard WordPress directives.
In that case, your external redirect should come before any WordPress routing directives. The RewriteEngine
directive only needs to appear once, anywhere, in the file. Although it is obviously more logical if it occurs once at the top.
You also need to remove the query string from the substitution, otherwise you'll get a redirect loop since the domain is the same. If the domain/host remains the same then this can be omitted from the substitution.
Try the following:
RewriteCond %{QUERY_STRING} tab=auto_data
RewriteRule ^polk/$ /automobile-data/? [R=301,L]
This specifically matches only the URL-path /polk/
(as mentioned in comments), unless this needs to be more general? And tab=auto_data
must match anywhere in the query string.
The ?
on the end of the substitution removes the query string and so prevents a redirect loop. (Presumably the query string should be removed from the target?) Although since the source and target URL paths are different, this is not strictly necessary anymore.
If the "domain remains the same", then there is no specific need to include the scheme and host in the substitution. Unless you are hosting multiple domains etc.?
Make sure the browser cache is cleared before testing as 301s are notorious for caching. Testing with 302s can be preferable for this reason.
UPDATE: To specifically remove this query string parameter, but copy the remaining query string onto the target, try something like:
RewriteCond %{QUERY_STRING} ^tab=auto_data(?:&(.+))?
RewriteRule ^polk/$ /automobile-data/?%1 [R=301,L]
(?:&(.+))?
- grabs any remaining query string (if any), but excludes the &
prefix (param delimiter) from the captured group. %1
is a backreference to this captured group.
Upvotes: 2