RewriteCond for query parameters in .htaccess files - .htaccess

I have to write some Rewrite Rules and I need to make a check based on my query parameters.
The public url is something like this abc.com/lmn/xyz.json and there is an optional parameter optparam.
This is what I want to achieve:
If optparam is present and not equal to false, the conditions have to fail and carry on with other rules.
After reading through few blogs and posts, I have a very faint idea about these rules. So tried this:
RewriteCond %{QUERY_STRING} !^optparam $ [NC,OR]
RewriteCond %{QUERY_STRING} ^optparam=false$ [NC]
RewriteRule ^lmn/xyz.json$ xyz.json
But the RewriteRule is applied even when I send the param value to be true.
Please tell me what I am missing here.
Thanks in advance!
Examples:
abc.com/lmn/xyz.json ==> Rule should fire
abc.com/lmn/xyz.json?optparam ==> Rule should not fire
abc.com/lmn/xyz.json?optparam=false ==> Rule should fire
abc.com/lmn/xyz.json?optPARAM=hfjsgzjrg ==> Rule should not fire
abc.com/lmn/xyz.json?optParam=FALSE ==> Rule should fire

Change your rule to this:
RewriteCond %{QUERY_STRING} ^(optparam=false)?$ [NC]
RewriteRule ^lmn/xyz\.json$ xyz.json [L,NC]

Well, I think an additional check of optparam not present at all is required. Tried this:
RewriteCond %{QUERY_STRING} (^$|!^optparam$|^optparam=false$) [NC]
RewriteRule ^lmn/xyz\.json$ xyz.json [L,NC]
This seems to work for me. But is there any way to further shorten this condition? I believe I am not using regex to its full potential here.

Related

remove second question mark from query string

I made a mistake in creating links. I corrected it but now still there are links floating around that might look like this:
http://www.domain.com/?page=1?date=29062015&id=778
I would like to correct this using the rewriteEngine to redirect my users to:
http://www.domain.com/?page=1&date=29062015&id=778
I searched around and tried the following, but it doesn't work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule ^\/ ^\/$1\&$2 [L,R=301]
What should I change here?
I've slightly corrected your rule,hope it will help to resolve the issue:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule (.*) $1?%1&%2 [L,R=301]
Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond (Query string condition).
Good article about rewriting query string here: https://wiki.apache.org/httpd/RewriteQueryString

Redirect olddomain querystring layout to newdomain and new querystring layout?

OK, say my blog site myurl.org as many links to a separate domain:
old.myurl.org?oldvar=foo
Only old.myurl.org no longer exists and has been replaced by new.myurl.org.
If the query string vars were the same on new.myurl.org, I believe I could rewrite from .htaccess using:
RewirteCond %{http_host} ^old.myurl.org$ [NC]
RewriteRule ^(.*)$ new.myurl.org [L,R=301,QSA]
The only problem is that I also need to change the query string var from oldvar to newvar and preserve it's data (foo).
There are plenty of examples of how to rewrite query string vars in different ways, but I can't seem to find an example of this scenario.
I need to rewrite:
old.myurl.org?oldvar=foo
To:
new.myurl.org?newvar=foo
Edit
Furthermore, I have several potential query string key values to account for, but not all will always be present.
So I may need:
old.myurl.org?oldvar=foo&oldvar2=bar --> new.myurl.org?newvar=foo&newvar2=bar
or
old.myurl.org?oldvar2=bar --> new.myurl.org?newvar2=bar
This may not even be possible, but in one case I need to strip off part of the query string value. So ?oldvar=foo{term} might need to become ?newvar=foo
Thanks again!
First condition match the domain, second condition match the query string and the rule will match your domain root:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1 [R=301,L]
You can place more than one query string as well:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$ [OR]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)&oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1&newvar2=%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
To use a path like x/y/z as you have mentioned on the comment you change the RewriteRule for example the rule we are currently using, will only redirect from yourdomain.com/?... which is:
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
If you want to catch a different path you would do this:
RewriteRule ^x/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above would catch yourdomain.com/x?... and yourdomain.com/x/?...
You can have more than one path as well and use a OR condition like this:
RewriteRule ^(x|x/y|x/y/z)/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above means we want to match x/?... OR x?... OR x/y?... and x/y/?... OR x/y/z?... OR x/y/z/?...
By encapsulating it on the parenthesis and using the | as separator and OR.
The ^ and $ means match from begin to end, so when it contains nothing means match nothing which means root folder domain.com/, when there is content it will match the content for instance domain.com/x or whatever you place into it.

Why is my pagination not working after mod rewrite?

Before rewriting my url file.php?style=foo&page=2 works fine.
After rewrite it stops working and just stays on the same page with the ?page variable at the end like it's not getting caught:
foo-new?page=2
After the rewrite the page parameter stops working on the clean url.
Here's what my rewrite rules look like:
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/?$ file.php?style=$1&redirect=no [NS]
So I translate what you'd like to do then, I change your rules and write them. Please tell me if I'm right or wrong.
You'd like to check if in the query string you have an optional parameter "style".
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
What is the point of the ^ and the /? and the $ ? Remove them:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)
It's clearer this way and should still work for what you want.
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
What is the point of the ? ? Remove it:
RewriteRule ^file\.php$ %1-new [NS,R=301,L]
So, please try these rules and tell me if they work, and if not, please be more specific or give real URL sample that you'd like to rewrite:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)$
RewriteRule ^file\.php$ %1-new [QSA,NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/$ file.php?style=$1&redirect=no [NS]

Rewrite htaccess old oscommerce links

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

mod_rewrite deny access if parameter equals something

I have urls like
mydomain.com?act=somethingbad
mydomain.com?act=somethingworse
and
mydomain.com?act=somethinggood
mydomain.com?act=somethingbetter
I need to make bad actions forbidden.
Tried something like:
RewriteCond %{QUERY_STRING} act=(.*)
RewriteCond %{QUERY_STRING} act=[^(somethinggood)]
RewriteRule ^(.*) - [F]
Because I have more bad actions than good I want to exclude de good actions from this rule.
But the above example doesn't work.
Use these rules:
RewriteCond %{QUERY_STRING} (^|&)act=(.*)
RewriteCond %{QUERY_STRING} !(^|&)act=(somethinggood|somethingbetter)(&|$)
RewriteRule ^(.*) - [F]
Parameter act can be any where (e.g. http://example.com/?act=somethinggood and http://example.com/?mode=happy&act=somethinggood and http://example.com/?mode=happy&act=somethinggood&extra=yes will be OK)
If you will have empty value for act parameter, it will be rejected (e.g. http://example.com/?act= is treated as BAD parameter)
Based on URL examples you have provided, this rule will be applied for ALL URLs that have act parameter in query string.

Resources