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!
2
Answers
I was able to come up with a solution that worked prior to the answer (above) I marked as correct as my solution isn't quite as elegant.
I did realize after a bit of more research I needed to use a RewriteCond with %{QUERY_STRING} for what I was trying to achieve.
Again not as elegant as the answer above...but here is my code:
This actual kept the url in the browser as it was input (/var/category/filter?subcat_filter_depth=) but showed the page as if it was opened with the proper filter for type=ALL and produced no errors both in browser and in Drupal dblog.
The above solution is more useful in explanation alone and gave me much better understanding of how to break up a URL and put it back together with modifications especially when dealing with strings that have variables that change like unique ID's.
Both the
RedirectMatch
(mod_alias) andRewriteRule
(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 theQUERY_STRING
server variable.This rule also needs to go near the top of the
.htaccess
file, before any existing rewrites.For example:
This redirects
example.com/vid/category/filter?subcat_filter_depth=<number>
toexample.com/vid/category/filter?type=All&subcat_filter_depth=<number>
.In
.htaccess
there is no slash prefix on the URL-path matched by theRewriteRule
pattern. ie.^vid/category
(not^/vid/category
).$0
is a backreference that contains the matched URL-path from theRewriteRule
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
becomestype=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.