URL rewriting with Isapi_Rewrite 2 on Windows shared hosting fails partially - asp-classic

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.

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]

How to correctly use QUERY_STRING in ISAPI rewrite?

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]

htaccess condition specific pages

I am having a little issue with my htaccess, what my goal is and what is currently working on my WordPress site is:
/blog/title-of-blog-post/1923
is being redirected to
/title-of-blog-post/
That is done with this rule:
RewriteRule ^blog/(.*)/.*/$ $1/ [R=301,L,QSA]
However, that means it is also redirecting
/blog/page/2/ to /page/
/blog/page/3/ to /page/
etc
Is there a condition to which I could stop the /blog/page/nr being redirected, but still keep those rules as they are?
Many thanks
You could add an RewriteCond before your rule.
RewriteCond %{REQUEST_URI} !^/blog/page/[0-9]+/?$
RewriteRule ^/?blog/(.*)/.*/?$ $1/ [R=301,L,QSA]
You could probably optimize it a bit if needed.
Hope it helps you forward.

site 5x faster via mod_rewrite, but CSS images are broken

I am using .htaccess to accelerate a site with the following redirects:
request for http://example.com/images/name.jpg routed to http://i.example.com/name.jpg
request for http://example.com/css/name.css routed to http://c.example.com/name.css
From listening to the Stack Overflow podcast, I learned that this could make a site faster, since the browser can download more files simultaneously (apparently two streams per domain, although this is unconfirmed).
Indeed, the difference is dramatic; the page loads about five times as fast!
I have not touched the original folders and images -- I am just using mod_rewrite to change the addresses from example.com/images/ to i.example.com/:
rewritecond %{HTTP_HOST} !^i\.example\.com [NC]
rewriterule ^images/([^/]+)$ http://i.example.com/$1 [L]
rewritecond %{HTTP_HOST} !^c\.example\.com [NC]
rewriterule ^css/([^/]+)$ http://c.example.com/$1 [L]
The problem I have is that this technique works perfectly for image tags included in html, but doesn't work for images included via stylesheets:
img src=/images/logo.jpg works perfectly
background:url(/images/logo.jpg); does not work
The server error log contains the following entry:
File does not exist: /var/www/html/css/images, referer: http://example.com/page.html
This seems to imply that the rewrite rule is being applied incorrectly.
The stylesheets work if I use:
background:url(http://i.example.com/logo.jpg);
However, in order to avoid rewriting all the style sheets, I'd like to know: why doesn't url rewriting apply to stylesheets the way it does to html img tags.
[update1] This problem exists in Safari 4 Beta, Firefox 3.0.3, and Chrome, but the page works perfectly in IE6.
[update2] Adding [L,R=301] and [L,R=302] did not help.
[update3] I tried the following based on Gumbo's suggestion below:
Redirect externally if path doesn’t match host name:
rewritecond %{HTTP_HOST} !^i\.domain\.com$
rewriterule ^images/([^/]+)$ http://i.domain.com/$1 [L,R=301]
rewritecond %{HTTP_HOST} !^c\.domain\.com$
rewriterule ^css/([^/]+)$ http://c.domain.com/$1 [L,R=301]
Redirect internally; if there's an unnecessary folder name remove it (see server error above):
rewritecond %{HTTP_HOST} ^i\.domain\.com$
rewriterule ^images/([^/]+)$ $1 [L]
rewritecond %{HTTP_HOST} ^c\.domain\.com$
rewriterule ^css/([^/]+)$ $1 [L]
It still didn't work. Bizarrely, the server error is:
File does not exist: /var/www/html/css/var, referer: http://domain.com/page.html
I was able to resolve this by not trying to incorporate directories into the subdomains:
request for domain.com/images/ routed to i.domain.com/images/
request for domain.com/css/ routed to c.domain.com/css/
It works perfectly and is still extremely fast.
There seems to be a bug in modern browsers where a css request that is redirected will apply only the new domain, leaving the original directories as part of the request:
If a css image at url(domain.com/images/name.jpg) is redirected to i.domain.com/name.jpg, the browser will mistakenly request i.domain.com/images/name.jpg.
I found a way to solve this problem if all host names use the same virtual host:
# redirect externally if path doesn’t match host name
RewriteCond %{HTTP_HOST} !^i\.example\.com$
RewriteRule ^images/([^/]+)$ http://i.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^c\.example\.com$
RewriteRule ^css/([^/]+)$ http://c.example.com/$1 [L,R=301]
# redirect internally to the file
RewriteCond %{HTTP_HOST} ^i\.example\.com$
RewriteRule !^images/ images%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^c\.example\.com$
RewriteRule !^css/ css%{REQUEST_URI} [L]
This will do the following:
http://example.com/css/foo externally to http://c.example.com/foo
http://c.example.com/foo internally to /css/foo
http://example.com/images/bar externally to http://i.example.com/bar
http://i.example.com/bar internally to /images/bar
As well as correcting mismatching paths and host names:
http://i.example.com/css/foo externally to http://c.example.com/foo
http://c.example.com/images/bar externally to http://i.example.com/bar
A mismatch occurs when the requested stylesheet http://example.com/css/foo is redirected to http://c.example.com/foo and an image URI reference like /images/bar inside the stylesheet is resolved from this new base URI and thus leading to http://c.example.com/images/bar instead of the initial http://example.com/images/bar.

Resources