Rewrite a url without effecting homepage - iis

Requested url(s):
http://domain.com/about
http://domain.com/contact
etc.
Actual location of the about page and the other pages aswell:
http://domain.com/folder/about.cshtml
http://domain.com/folder/contact.cshtml
- The requested url should rewrite to the actual location.
- But the pattern shouldn't match: http://domain.com !
Else it would unstyle the homepage.
Here is my try at it:
<rule name="friendly_url">
<match url="(.+)/([-a-z]+)" />
<action type="Rewrite" url="http://{HTTP_HOST}/folder/{R:2}\.cshtml" />
</rule>
Obviously this will effect the homepage.
Even worse; it doesn't work at all. I can't get the rewrite to work properly.
Any contribution to the solution is much appreciated :)

Related

IIS Redirect With Parameters

Good day,
I have a URL as such:
http://www.mywebsite.com/2017/12/28/article-name/News/19391
Assuming
$1 = /2017/12/28/article-name
And $2 = /19391
How can I set up a redirect rule to redirect
http://www.mywebsite.com/$1/News/$2
to http://mywebsite.com/redirects/?path=$1&id=$2?
I'm not IIS fluent at all, so any help would be greatly appreciated! Thank you so much.
You need to follow this steps:
Create redirect which will match your URL. In your case, regexp can be ^(\d{4}/\d{2}/\d{2}/.*)/News/(\d+)$
Create the URL which you want to receive as redirect /redirects/?path=/{R:1}&id=/{R:2} where {R:1} is 2017/12/28/article-name and {R:2} is 19391
Combine it in one rule
At the end, your rule should be like that and you should place it in your web.config:
<rule name="news redirect" stopProcessing="true">
<match url="^(\d{4}/\d{2}/\d{2}/.*)/News/(\d+)$" />
<action type="Redirect" url="/redirects/?path=/{R:1}&id=/{R:2}" appendQueryString="false" />
</rule>

URL Rewriting IIS 7 Querystring Issue

I'm running a legacy website which has a tonne of 301 redirects on it. In an effort to make my life simpler, I switched them from doing manual 301 redirects in IIS to using the url rewriting module and a rewrite map, which I can generate from the database.
Unfortunately I've now hit a snag, in that some of their urls come with querystrings (from Google's adwords), and these invariably 404 (because they don't match the urls in my rewrite map).
Is there a way around this, or do I have to go back to manually doing the 301s?
For the record: we're using IIS7
Code samples:
url: http://mydomain.com/widgets/?gclid=[google code]
Rewrite Rule:
<rule name="Rewrite rule1 for News301Redirects" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{News301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}" appendQueryString="false" />
</rule>
Map Line:
<add key="/widgets/" value="/widgets.asp" />
Well, the answer proved to be depressingly simple - just an absolute pig to find.
Instead of using REQUEST_URI (which gets the whole requested URL, including the querystring) I've switched to using URL (which just gets the URL without the querystring) and that seems to have utterly resolved my problems.

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.

IIS URL Rewrite module unencoding query string

We have a really simple URL rewrite rule that basically causes a 301 redirect to another domain:
<rewrite>
<rule>
<match url="^(default.aspx)?$" />
<action type="Redirect" url="https://some.other.domain.com" />
</rule>
</rewrite>
Unfortunately, when a URL like this is encountered:
http://original.domain.com/?returnUrl=https%3A%2F%2Fsome.url%2F
The redirect URL looks like this:
https://some.other.domain.com/?returnUrl=https://some.url/
Notice how the URL encoding is lost.
Is this a bug in the URL rewrite module? If so, how can one work around it?
I figured it out. The rewrite rule has only a partial redirect URL specified. Query string encoding is now preserved now that I changed this:
<action type="Redirect" url="https://some.other.domain.com"/>
...to this:
<action type="Redirect" url="https://some.other.domain.com/"/>
(note the trailing slash)

Resources