Is there something wrong with my rewrite code in .htaccess file - .htaccess

I have been demented with this problem for 3 days and no matter how differently I search Google or read the Helion website I cannot understand what I am doing wrong. So, I do hope that someone will be kind enough to help me.
I use a .htaccess file to create search engine friendly URLS and it has worked well for several years.
My web hosting company has now moved my site to a new Windows IIS8.5 server and the rewrites no longer work. They tell me that are using Helicon Ape (not sure what that is). But, I cannot find anything on the Helicon website that makes sense to me, as it is too technical for my old pensioners mind.
Here is a sample of the code that used to work on the old server:
# Helicon ISAPI_Rewrite configuration file
RewriteEngine on
RewriteBase /
RewriteRule ^uk-bed-breakfast-(.*)\.htm /bed-and-breakfast/region.asp\?county=$1 [QSA]
This used to turn
www.mydomain.com/bed-and-breakfast/region.asp?county=first
into
www.mydomain.com/uk-bed-breakfast-first.htm
Now all I get when I use the second URL is a 404 error.
Do I need to make any changes to comply with the newer version of IIS or Helicon Ape Rewrite, and if so what are they

After a lot of painstaking experimentation the problem has at last been solved.
It seems that with the new iis8.5 server and Helion Ape I needed to put the rewrite rules into a web.config file instead of a .htaccess file.
The new rule for the example I gave in the question is:
<rule name="Imported Rule 6">
<match url="^uk-bed-breakfast-(.*)\.htm" ignoreCase="false" />
<action type="Rewrite" url="/bed-and-breakfast/region.asp?county={R:1}" appendQueryString="true" />
</rule>
Using this system the website is at last functioning correctly.
Many thanks to all who tried to help.
Tog

Related

How to serve static file (json) to a explicit path

I want to add a path mysite.com/_this when a request for this path is called I want to serve the json file that's under Sites/MySite/Content.
The caveat is I cannot implement this via code change. I need a way to implement this setup using the IIS CLI.
I have found the following article https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing, but it's a little cryptic and hard to follow.
Windows: Window Server 2016 (1607)
IIS: 10.0.14393.0
In my opinion, the right solution for your issue is iis URL rewrite module.
If you did not install the url rewrite module you could download from the below link:
URL Rewrite
then add a rule in web.config file:
<rule name="Json Redirect" stopProcessing="true">
<match url="([\S]+[.](json))" />
<action type="Rewrite" url="folderpath/{R:1}" />
</rule>
You can do all these changes in the IIS Manager UI, but if you want to script them, look into the PowerShell IIS cmdlets.

IIS URL Rewrite down to a subfolder

I need to get the IIS URL rewrite module to work but am having no luck.
What I need to do is 'inject' a subfolder (psms) into a url when it is not present, like so:
https://something.somewhere.com/acp/default.aspx?k=v
Doesn't have the subfolder psms so redirect to https://something.somewhere.com/psms/acp/default.aspx?k=v
OK, psms is a root folder of hosting ;
you need to use expression like
<action type="Rewrite" url="acp/default.aspx?k={R:1}" />

Getting 404 errors using Slim PHP on IIS7

I'm trying to use the Slim PHP framework with IIS7. Now on some routes I keep getting a 404 from IIS which I find really confusing. Here's my index php:
require 'slim/slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->notFound(function () use ($app) {
echo("not found!");
});
$app->get("/books/:id", function($id){
echo("hello book " . $id . "!");
});
$app->get("/test",function(){
echo("testing");
});
$app->run();
On IIS (running PHP 5.3) I've setup the url rewritting to send all requests to my index.php file and disabled directory browsing. This seems to work fine as going to localhost/books/123 produces "hello book 123!". But when I go to localhost/test I just get a 404 error from IIS! If add a trailing slash to that url my slim notFound handler is triggered instead of my route handler.
If i create a directory called "test" it stops the 404 response but triggers the slim notFound handler rather than the defined route handler as I would expect. This seems really weird as I've disabled directory browsing.
I know my slim notFound handler is working ok as trying localhost/abc/xyz triggers the handler and works as expected.
Could anybody explain what is going on and why? Have I overlooked some configuration?
Thanks.
It turns out this weird behaviour was caused by my url rewrite rule on IIS. Doh!
I had the regex rule /.* rewrite to my index.php file rather than use .* as the regex.
The first slash wasn't being included as part of the rewritten url which then caused the issue I described. Changing the regex to just be .* fixed everything.
If anyone else encounters an issue like this then double check your url rewrite rule even if it seems to be working ok!
My very simple web.config file is:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="slim catch all" enabled="true">
<match url=".*" />
<action type="Rewrite" url="/index.php" />
<conditions>
<add input="{URL}" pattern="/content/.*" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
Note: this is using URL Rewrite 2.0 on IIS
Index.php is the file which sets up all the Slim routing. The condition is stopping requests to the /content/ path from being routed through Slim - this makes it a good place to store things like images, css files and javascript as the responses should be faster as they are not processed by Slim.

.htaccess redirect to subdirectory on Verio windows server

I've been trying to get a website up an running on my work's servers using Verio windows server 2003 gold and they've been quite useless when it comes to helping me out.
All I'm trying to do is set up a Wordpress installation, which they apparently require to be in a subdirectory. This is done and I have it in domain.com/wp ready to go. At this point they recommend that I use an .htaccess file to redirect to that url, so as they just sent me basically an "About .htaccess" link, that was of no help.
Did some searching, found a couple options like this one:
http://www.site5.com/blog/apache/htaccess/redirect-to-blog/20090427/
and nothing I use seems to work, not even as a straight redirect. Just gives me a "Directory Listing Denied - This Virtual Directory does not allow contents to be listed."
I do want to mention I did enable URL Rewrite for IIS via ISAPI_Rewrite 3 in the control panel.
As I mentioned Verio support is useless and I can't seem to find anything searching that seems to help my situation.
So pretty much what I need is domain.com/wp to rewrite as domain.com so all links centered around it do the same (ie domain.com/about domain.com/contact)
Any help? Thanks
If I understand you correct you need your Wordpress application to be available from the root of your web site instead of /wp/ folder? Then please try following .htaccess:
RewriteEngine on
RewriteRule ^(?!wp/)(.*) /wp/$1 [NC]
By the way I think you could try simply copying /wp/ folder content to the root folder.

IIS 7 Url Rewriting and two domains

I was writing up this question and in the process, it forced me to think a little harder and I answered it myself, though I still don't completely understand why it solved it.
I have an account on a shared host with 2 domains registered. I'm using the Asp.Net stack to run a few things like a blog and another site I am planning to kick off eventually. Both of my domains point to the root; the first is the original I used to signup, the second is a root domain pointer I added. Here is how I want it to behave:
Directory Structure:
Root (www.domain1.com)
Root --\ Blog (www.domain1.com/blog)
Root --\ Site2 (should be directed here if www.domain2.com)
Root --\ Site2 --\ Junk (www.domain2.com/junk)
Right now, if you type in www.domain1.com or www.domain1.com/blog, that behaves as expected and I am fine with that. For www.domain2.com, I have the rewrite rule configured like this(from the web.config):
<rule name="Domain2">
<match url="(.*)(/)?" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="(www\.)?domain2\.com" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="/site2/{R:1}" />
</rule>
That rule is supposed to match any path if the host is domain2.com, pick off the path to the requested resource and format it properly. So when somebody types www.domain2.com/junk/default.aspx, in IIS, this resolves to www.domain2.com/site2/junk/default.aspx without the user ever knowing. This is mostly working as advertised except when the user does not type a trailing slash in a subfolder. IE:
www.domain2.com (works)
www.domain2.com/ (works)
www.domain2.com/junk/ (works)
www.domain2.com/junk (doesn't work!) IIS 7 looses its brain here and formats it out like www.domain2.com/site2/junk because a 2nd request is automatically issued for the trailing slash and a 404 happens.
So, I updated the action to be:
<action type="Rewrite" url="/site2/{R:1}/" />
This seems to have resolved it, but why doesn't IIS 7 now spit out www.domain2.com/junk2/default.aspx/ ? How does it know not to append a trailing slash to a document extension?
www.domain2.com/junk does not work because you added slash in match, but actual url to match does not contain it. It just contains "junk".
Also you need to add:
<rule name="Domain2" stopProcessing="true">
so it does not evaluate other rules if match is found. I suspect that you may see that what confuses you, is other rules you have setup.

Resources