RewriteCond Query String condition not working - .htaccess

I am trying to implement some URL rewrites and I'm running into an issue. The query string condition does not seem to be getting a match.
Example URL:
http://www.domain.com/ProductDetails.asp?ProductCode=498688
Here is the rule I have written.
RewriteCond %{QUERY_STRING} ^ProductCode=(.*)$
RewriteRule (.*) /catalogsearch/result/?q=%1 [R=301,L]
I have another rule that's almost identical that works. Any thoughts on why this one is not firing?

Related

Htaccess 301 redirect with query string params

This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive
You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.

How to rewrite query string to hash in the URL?

I am looking to rewrite:
www.mysite.com/services/category1/?content=service1
to
www.mysite.com/services/category1/#tab-service1
I have this specific rewrite that I want to generalize for all the possible URLs on the site:
RewriteCond %{QUERY_STRING} content=service1
RewriteRule ^services/category1 services/category1/#tab-service1? [NE,R,L]
I tried the following but it didn't work :( -
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?content=(.*)$ services/category1/#tab-$1 [NE,R,L]
Thanks.
You rule looks fine except for 1 thing
QUERYSTRING content=service1
isn't part of match in RewriteRule's pattern so you can't test query string (Url part after the ?) in pattern of a RewriteRule. To test url query string we use %{QUERY_STRING} variable like the one you are already using.
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?$ services/category1/#tab-%1 [NE,R,L]

.htaccess not working even though other rules work

I am trying to do a redirect to another site. The following rule works perfectly for all the other rules within my .htacces but the following rule does not seem to work and i can'r figure out why, after lots of try and error.
RewriteCond %{HTTP_HOST} ^www.oldexample.com$
RewriteRule ^folder3/page.php?value=(.*)$ http://newexample.com/folder1/page.php?value=$1 [L,R=301,NC]
I tried the following which redirected but without the value within (.*) added
RewriteCond %{HTTP_HOST} ^www.oldexample.com$
RewriteRule ^folder3/page.php?(.*)$ http://newexample.com/folder1/page.php?$1 [L,R=301,NC]
Could this be an issue with the = symbbol? I tried escaping it and escaping the . also but to no avail
You can't match against the query string (everything after the ?) in a rewrite rule, you need to use a RewriteCond and match against the %{QUERY_STRING}. However, it looks like you're just trying to pass the query string along, unchanged. So you don't need to mess with any of it:
RewriteCond %{HTTP_HOST} ^www.oldexample.com$
RewriteRule ^folder3/page.php$ http://newexample.com/folder1/page.php [L,R=301,NC]
The query string will get appended automatically. You need to make sure this rule is above any routing rules as it needs to take place before any internal rewrites.

.htaccess redirect query string value to another key

What i'm trying to do is rewrite the following pattern
http://example.org/path?articleid=5657
to this pattern:
http://example.org/path?p=5657
Essentially it comes down to changing the key to the url parameter, i have searched extensively with no clear example of how to do this exact thing using only htaccess rewrite rules.
i've tried this general approach with no luck
RewriteCond %{QUERY_STRING} ^articleid=([0-9]*)$
RewriteRule ^articleid=([0-9]*)$ /?p=$1 [R=301,L]
You can't match against the query string in the pattern of a rule, you need to match against the URI:
RewriteCond %{QUERY_STRING} ^articleid=([0-9]*)$
RewriteRule ^path$ /path?p=$1 [R=301,L]

mod_rewrite remove query string on top of a rewrite rule

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.
This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

Resources