I'm having some real issues getting a redirect to work in .htaccess
with drupal 7.
I have links like this:
example.com/vid/category/filter?subcat_filter_depth=125
and I need them to go to: (on same domain)
example.com/vid/category/filter?type=All&subcat_filter_depth=125
after =
the number may be different like 67 or 32 etc. meaning that can be any number and whatever number is at the end of the original link needs to also be at the end of the new link.
I think the fact that the link is pretty much the same path just different filter is what is causing the issue as even the Drupal redirect url module will not allow it at an attempt to add them one at a time as they occur.
here is everything I've tried:
RedirectMatch 301 ^/vid/category/filter?subcat_filter_depth=(.*) /vid/category/filter?type=All&subcat_filter_depth=$1
RewriteRule ^(.*)/vid/category/filter?subcat_filter_depth=(.*) $1/vid/category/filter?type=All&subcat_filter_depth=$2 [R=301]
RedirectMatch ^/vid/category/filter?subcat_filter_depth=(.*) ^/vid/category/filter?type=All&subcat_filter_depth=$1
RedirectMatch 301 ^(.*)/filter?subcat_filter_depth=(.*) ^$1/filter?type=All&subcat_filter_depth=$2
RedirectMatch ^/(.*)/filter?subcat_filter_depth=(.*) ^/$1/filter?type=All&subcat_filter_depth=$2
RewriteRule "^/(.*)/filter?subcat_filter_depth=(.*)" "^/$1/filter?type=All&subcat_filter_depth=$2"
mod_rewrite is on and there is an AllowOverride pathed/to/the/.htaccess file under <Directory>
inside <VirtualHost>
in the 000-default.conf
running on Debian Apache ..pretty much latest versions.
I know mod_rewrite is working because if I add a bad rule or rewrite with a syntax error I get error 500 response page.
I've searched and tried just about everything I can think of and nothing seems to work.
Any help getting this sorted and functioning would be greatly appreciated!
Both the RedirectMatch
(mod_alias) and RewriteRule
(mod_rewrite) directives match against the URL-path only, which notably excludes the query string.
To match against the query string you need to use RewriteRule
with an additional condition (RewriteCond
directive) that checks against the QUERY_STRING
server variable.
This rule also needs to go near the top of the .htaccess
file, before any existing rewrites.
For example:
RewriteCond %{QUERY_STRING} ^subcat_filter_depth=\d+$
RewriteRule ^vid/category/filter$ /$0?type=All [QSA,R=302,L]
This redirects example.com/vid/category/filter?subcat_filter_depth=<number>
to example.com/vid/category/filter?type=All&subcat_filter_depth=<number>
.
In .htaccess
there is no slash prefix on the URL-path matched by the RewriteRule
pattern. ie. ^vid/category
(not ^/vid/category
).
$0
is a backreference that contains the matched URL-path from the RewriteRule
pattern.
QSA
causes the original query string (eg. subcat_filter_depth=125
) to be appended to the query string stated in the substitution string, ie. type=All
becomes type=All&subcat_filter_depth=125
.
Test with 302 (temporary) redirect to avoid potential caching issues. Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed it works as intended.
You will likely need to clear your browser cache before testing.