IIS ReWrite Rule for removing url segment - iis

I am trying to redirect requests to my site from:
pbt.mysite.com/pbt/index.php?querystring....blahblahblah..
so I'd like to remove the /pbt/ segment of the url and keep the remaining query
to:
pbt.mysite.com/index.php?querystring....blahblahblah..
I've setup an IIS Rewrite rule with the following:
<rewrite>
<rules>
<rule name="Rewrite remove PBT segment from URL">
<match url="^/pbt/([/_0-9a-z-]+)"/>
<action type="Rewrite" url="{R:1}"/>
</rule>
</rules>
</rewrite>
But this doesn't seem to work..
I'm wondering if there is a confusion in my rule because the subdomain is the same as the segment I want to remove (but keep the subdomain)
Additionally, this is a drupal site hosted in IIS so there may be a better way to do this in php (which I know nothing about)
Any ideas?

You have a leading slash that needs to be removed. Also, if you want to match on files and folders (not just folders as in your example), change the part between the parens too like so:
<match url="^pbt/(.*)$"/>

Related

How to redirect URL's in IIS

I have a website setup in IIS, current working URL is http://hr.something.com/ess
I need to have a redirect, so that if the users type in http://hr.something.com they will still end up at the /ess page.
Currently just typing in http://hr.something.com gives the default IIS landing page.
Everything i've found online talks about adding/removing trailing slashes or doing rewrites (which i dont think is what is needed here)
Apologies if there are already answers for this, but im sure im using the wrong terminologies, leading me to the wrong answers.
you should install the iis rewrite module
and see the
https://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
create the rediretly rule and you could test in iis manager
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>

Using web.config files to rewrite for my program

I am looking for a way to use the URL Rewrite module to make the URLs for my program look cleaner.
I have been looking into IIS rewrite for a while now and whenever I get to the point where I think I understand it, nothing works.
I have this layout for my program.
etreecycle.co.uk/
- This has nothing to do with my program, this is a store for another project. (Wordpress)
etreecycle.co.uk/websites/MPW/
- My program is installed here.
etreecycle.co.uk/websites/MPW/index.php
- I want the first section of the domain to be put here, for example.
etreecycle.co.uk/websites/MPW/index.php?page=posts&action=1234
- I want this url to look like this:
etreecycle.co.uk/websites/MPW/post/1234
I also have an index.php in the directory etreecycle.co.uk/websites/MPW/admin/
This should take the same layout.
For example;
etreecycle.co.uk/websites/MPW/admin/?page=settings&action=save
- should look like;
etreecycle.co.uk/websites/MPW/admin/settings/save
Can anyone help me achieve this with IIS URL Rewrite? Thank you.
Here's an example:
<rewrite>
<rules>
<rule name="home" stopProcessing="true">
<match url="^settings/(\S+)$" />
<action type="Rewrite" url="admin/?page=settings&action={R:1}" />
</rule>
</rules>
</rewrite>

Redirecting a subfolder to subdomain and keep the url intact

I want to (temporary (few months tops)) redirect a subfolder to a subdomain and keep the url intact. This because we are moving our website to different server bit by bit.
We currently have multiple sub applications in IIS like:
www.ourdomain.com/shoppingcart
www.ourdomain.com/backoffice
We want to redirect those to their subdomains:
shoppingcart.ourdomain.com
backoffice.ourdomain.com
To do this we added the following rule to our web.config:
<rule name="MovedShoppingcart" stopProcessing="true">
<match url="^shoppingcart/([_0-9a-z-/]+)" ignoreCase="true" />
<action type="Redirect" url="http://shoppingcart.ourdomain.com/{R:1}" appendQueryString="true" />
</rule>
This rule works fine for a few samples, but not for sub folders:
www.ourdomain.com/shoppingcart --> shoppingcart.ourdomain.com
www.ourdomain.com/shoppingcart/foo --> shoppingcart.ourdomain.com/foo
The above 2 are fine, but when I add another subfolder it doesn't work:
www.ourdomain.com/shoppingcart/foo/bar?id=temp --> shoppingcart.ourdomain.com/foo?id=temp
(the missing /bar/)
What is wrong with my rewrite rule?
When entering the regular expression in regexr the tool indicated an error.
By escaping the forward slash as follows the regex should work fine.
^shoppingcart\/([_0-9a-z-/]+)

IIS 7 Can you rewrite a rewrite

I am trying to get something like this working with iis 7 microsoft rewrite plugin.
if user requests...
/author/bob/ - show bobs author page
if user requests ...
/author/bob/rss - show authors rss page by rewriting it to
/author/bob/?rss
however i want to to do it in a generic way for urls that end with /rss e.g. for 2 path parts...
<rule name="rss 2 dir listing" stopProcessing="false">
<match url="^([^/]+)/([^/]+)/rss$" />
<action type="Rewrite" url="/{R:1}/{R:2}/?rss" appendQueryString="true" />
</rule>
so the above rule would have to go through to another rewrite rule i.e the one that maps to the author page.
but it looks like this now tries to may to a static directory i.e. the 404 error output is
Requested URL http://site.l:80/author/bob/?rss
Physical Path C:\Users\Workspace\site.l\author\bob\
so its looking for a static directory but you can see its rewritten in to ?rss and trying the url http://site.l:80/author/bob/?rss in the browser works
I am using iis with railo tomcat so it could be a connector issue
Here is the current author rule with the rss incorporated ideally i want to remove adding rss per rule and use generic rules above.
<rule name="author item" stopProcessing="false">
<match url="^author/([a-zA-Z0-9-]+)/(rss)?$" />
<action type="Rewrite" url="/index.cfm?event=AuthorController.getItem&name={R:1}&{R:2}" appendQueryString="true" />
</rule>

IIS rewrite module 301 redirects remove folder adding extra slash

I have this IIS rewrite rule
<rule name="Redirect rule for aliased Pages">
<match url="^/pages/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
The idea is that it should redirect http://mydomain.com/pages/about-us/ to http://mydomain.com/about-us/. According to the tester in IIS in that case R:1 is about-us/ but the redirect always goes to http://mydomain.com//about-us/ (see double //). I have tried removing both forward slashes from the match URL and leaving each one in on its own and it does not seem to make any difference.
Any idea where that extra / is coming from?
The URL rewrite module starts checking after the initial trailing slash, so the following would remove the first level folder "pages":
<rule name="Redirect rule for aliased Pages">
<match url="^pages/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
Testing with:
http://example.com/pages/about-us/ :: 301: http://example.com/about-us/
http://example.com/pages/advertising/ :: 301: http://example.com/advertising/
As expected.
Note that if you have the rewrite rules in a separate file, you will need to recycle the app pool or restart IIS for the changes to work.
Also, order of the rules is important too.

Resources