How to correctly use QUERY_STRING in ISAPI rewrite? - asp.net

I'm having a difficult time with this and hope you guys can help.
Using Helicon ISAPI Rewrite version 3.1.0.104 on our IIS server. I edit the http.conf file and have tried everything and still fail.
This is what I am trying to do:
Redirect this url:
https://www.domain.com/switch-by-version?version=2.8.5.2594
to:
https://test.domain.com/load/load.aspx?tver=2.8.5.2594
The version number at the end from the source url will change, and I need the target url to have the same version number at the end of its url as the example above shows.
I tried the following but it does not work:
RewriteCond %{QUERY_STRING} ^version=(\d\d?)\.(\d\d?)\.(\d\d?)\.(.*)$ [NC]
RewriteRule ^/switch-by-version(.*)$ https://test.domain.com/load/load.aspx?tver=%1 [R=307,NC,L]
Any help would be greatly appreciated!
Thanks.

If it's running in the equivalent context of an .htaccess, then the rule shouldn't start with a /. Here's a simpler version:
RewriteCond %{QUERY_STRING} ^version=(\d+\.\d+\.\d+(?:\..+)?)$ [NC]
RewriteRule ^switch-by-version/?$ https://test.domain.com/load/load.aspx?tver=%1 [R=307,NC,L]

Related

.htaccess 301 with querystring and special characters

I've looked around the site for answers and followed a few examples but i'm still drawing a blank with this one.
I'm trying to get the following to redirect.
www.mydomain.com/?s=flat+roof+specifier+checklist
to
www.mydomain.com/resources/flat-roof-checklist/
My rewrite rule is below.
RewriteCond %{QUERY_STRING} s=flat+roof+specifier+checklist
RewriteRule ^(.*)$ /resources/flat-roof-checklist/? [L,R=301]
I think the issue im having is the "+" character.
if i change my URL to:
www.mydomain.com/?s=flat%20roof%20specifier%20checklist
and my rewrite condition to:
RewriteCond %{QUERY_STRING} s=flat%20roof%20specifier%20checklist
It works fine.
The problem with that is, the URL HAS to be www.mydomain.com/?s=flat+roof+specifier+checklist (with the + characters in there) as the business has already sent out communications with that particular url, so unfortunately i can't change it.
Am i missing some sort of regx to format my url beforehand.?
There's already a 301 redirect plugin running on the site but it doesn't work with that either.
Apologies as i'm fairly new to php,wordpress,htaccess etc.
Any help will be great appreciated.
You can place the pattern into a regex capture group and escape the plus signs, tested and working on my test server with the following
RewriteCond %{QUERY_STRING} (s=flat\+roof\+specifier\+checklist)
RewriteRule ^(.*)$ /resources/flat-roof-checklist/? [L,R=301]

Can you have a conditional in the htaccess file

Been looking around the web to add a production rule in the .htaccess file. I have a wordpress website; one for production and the other, staging.
When a file is uploaded, it goes to AWS (s3). I need to prevent this behaviour for staging.
The code that sets the asset path is, in the .htaccess file:
RewriteRule ^wp-content/uploads/(.*)$ https://s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
I cant seem to find an "if statement" or some sort of condition to use. Honestly, I think this is not possible. Is it?
I only need to run that code for production and not staging. Staging url is different from production.
Updated
Whenever I use below, my website crashes:
<If "-z req('Host') == 'www.<PRODUCTION>.com/'">
RewriteRule ^wp-content/uploads/(.*)$ https://s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
</If>
If directive works on Apache 2.4 and newer versions. On lower versions you can use RewriteCond directive to conditionally rewrite urls.
You can use something like this
RewriteEngine on
#if host == "www.production.com"
RewriteCond %{HTTP_HOST} ^www.production.com$ [NC]
# execute the rule
RewriteRule ^wp-content/uploads/(.*)$ https: //s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]

Apache HTAccess Remove Query String for redirect

It sounds like a relatively simple one but here goes.. I have a wordpress website called example.com It has recently been redeveloped using wordpress however the old sites structure used the following www.example.com/?page_id=22 I want to redirect anything after the domain name that contains /?page_id=[any digit] to the homepage, im having great difficulty in accomplishing this and I also think I may have issues because wordpress itself uses the query string page_id before the permalinks redirects kick in, I could be wrong though but any help on removing the query string directly after a domain name would be great especially keeping in mind that it is a wordpress install.
I have tried various iterations that look like the following to no avail
RewriteCond %{QUERY_STRING} ^page_id=(.*)$
RewriteRule (.*) http://www.example.com/ [R=301,L]
RedirectMatch 301 ^/page_id=(.*)$ /
Try this rule right at top, just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} ^page_id=\d+$ [NC]
RewriteRule ^/?$ /? [R=301,L]
/? will strip off any query string.

RewriteRule to preserve querystring

I'm trying to make a redirect rule which follows this logic:
source: www.site.com/businessinfo.asp?accommID=123
destination: www.site.com/redirect.php?type=property&id=123
My code:
RewriteRule ^/businessinfo.asp$ /redirect.php?type=property&id=$1 [QSA]
I've tried a dozen different variations, but can't get anywhere. Can anyone help?
This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /businessinfo\.asp\?accommID=(.*)\ HTTP
RewriteRule ^ /redirect.php?type=property&id=%2 [R,L]
it should redirect www.site.com/businessinfo.asp?accommID=123 to www.site.com/redirect.php?type=property&id=123
Thanks for the above. For future reference, we solved this with a simple one-liner...
Redirect 301 /businessinfo.asp http://www.site.com/redirect.php
The querystring appends anyway, so we just rewrote the redirect script slightly to deal with accommID instead of ID.

URL rewriting with Isapi_Rewrite 2 on Windows shared hosting fails partially

I have been forced to switch from IIRF to Isapi_Rewrite due to moving to a shared hosting environment from a VPS. The URL rewrites I was using under IIRF should work with minimal modifications, but for some unknown reason only one of the rewrites works.
Here's my httpd.ini for ISAPI_REWRITE V2:
[ISAPI_Rewrite]
RewriteEngine On
RewriteRule /buy-sell-assets$ /search/Assets.asp
ReWriteRule .*trucks-tractor\.html$ /search/NewSearch.asp?cat_id=157
RewriteRule .*trucks-rigid\.html$ /search/NewSearch.asp?cat_id=10
ReWriteRule .*trailers\.html$ /search/NewSearch.asp?cat_id=4
ReWriteRule .*helicopters\.html$ /search/NewSearch.asp?cat_id=481
ReWriteRule .*aircraft-fixedwing\.html$ /search/NewSearch.asp?cat_id=467
ReWriteRule .*aircraft-fixed-wing\.html$ /search/NewSearch.asp?cat_id=467
ReWriteRule .*buses\.html$ /search/NewSearch.asp?cat_id=3
ReWriteRule .*boats\.html$ /search/NewSearch.asp?cat_id=559
ReWriteRule .*cars\.html$ /search/NewSearch.asp?cat_id=8
ReWriteRule .*crushers\.html$ /search/NewSearch.asp?cat_id=635
ReWriteRule .*screens\.html$ /search/NewSearch.asp?cat_id=637
ReWriteRule .*cranes\.html$ /search/NewSearch.asp?cat_id=430
ReWriteRule .*equipment\.html$ /search/NewSearch.asp?cat_id=12
The really odd thing is that the rewrite rule
RewriteRule .*trucks-rigid\.html$ /search/NewSearch.asp?cat_id=10
works, but none of the others do. They are all pointing to the exact same script, so I'm at a complete loss as to why the others aren't working.
Here's the working URL:
http://www.atn.co.za/buy-sell-assets/trucks-rigid.html
Here's one of the non-working URLs:
http://www.atn.co.za/buy-sell-assets/trailers.html
Why should one work and not the others? This is an incredibly frustrating issue, I've been trying various permutations for about four hours now and I'm about to do something drastic :(
If anyone can provide any insight or assistance on this I'd really appreciate it.
Try using the following format:
RewriteRule /buy-sell-assets/trucks-tractor\.html$ /search/NewSearch.asp?cat_id=157 [I,L]
make sure each rule ends with [NC,L] or you'll have all your pages pointing at one script.

Resources