0

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 :)

Nikita
  • 609
  • 2
  • 7
  • 18
  • Do you want to match sub folders too? For example, if you have `http://domain.com/contact/location`, do you want the same rewrite rule to be applied? – cheesemacfly Feb 24 '13 at 17:46
  • No, just `http://domain.com/contact`. – Nikita Feb 24 '13 at 18:04
  • What if you replace `url="(.+)/([-a-z]+)"` with `url="(.+)/([a-z]+)"` and `` with `` (you can even remove the `.cshtml` part)? – cheesemacfly Feb 24 '13 at 18:27
  • That will still match with `domain.com` so it is not a solution. – Nikita Feb 24 '13 at 18:29
  • How about `url="^/[a-z]+/?$"`? – cheesemacfly Feb 24 '13 at 18:38
  • @faa No, it is an IIS express server atm. It will soon be an IIS 7.5 when I publish the website. – Nikita Feb 24 '13 at 18:45
  • @cheesemacfly `[a-z]+/?$` is the right match, though what about the action? I tried `domain.com/pages/{R:0}\.cshtml` but it gives the 404 as in the previous question. – Nikita Feb 24 '13 at 18:52
  • `` should do (you can even remove the `.cshtml` part). Try with a redirect first if you want to see the result. – cheesemacfly Feb 24 '13 at 18:57
  • Alright, so then we get to the last part: how to avoid all the pages being unstyled? For some reason they do not pick up stylesheet and other files, though the location should be the same as the actual file. – Nikita Feb 24 '13 at 19:01
  • `url="^/[a-z]+/?$"` won't match any subfolder. So let's say you request `http://domain.com/about/css/main.css` but your real file is in `http://domain.com/folder/about/css/main.css`, your request won't be rewritten/redirected. – cheesemacfly Feb 24 '13 at 19:06
  • @cheesemacfly Not sure about that, though all that aside. I am using .net, so I targetted the css with an absolute position. Like so: `~/css/main.css`. That is the main reason why I am so confused. – Nikita Feb 24 '13 at 19:39
  • If you analyse your http trafic you will see what I mean (with fiddler for example). Your css full path will be like `http://domain.com/about/css/main.css` and if you try to reach it, you will see that it doesn't redirect/rewrite – cheesemacfly Feb 24 '13 at 21:27
  • You don't get it. The location of the stylsheets is in `~/css/main.css` that equals `domain.com/css/main.css` though the match pattern = `[a-z]+/?$` so `{R:0}` becomes `css` instead of `about` etc. The only way of fixing this is altering the pattern so that it doesn't match anything else instead of those pages. – Nikita Feb 24 '13 at 22:21
  • `/css/main.css` does not trigger `url="^/[a-z]+/?$"`. `/about/` does. – cheesemacfly Feb 24 '13 at 22:27
  • It does actually, `[a-z]+/?$` that is. `^/[a-z]+/?$` doesn't trigger anything though. Use IIS manager or something to test the pattern. You'll notice that the pattern matches `css`. That means that it will also be rewritten to `/folder/...`. All I can do is make a rule individually for every page. I just can't come up with a pattern that'll work the magic. – Nikita Feb 24 '13 at 22:32
  • I am surprised because here it seems to be ok: http://rubular.com/r/L20SrPWNF4 I won't have a IIS manager available until tomorrow...but will run some test when I can! – cheesemacfly Feb 24 '13 at 22:36
  • I just tested with the IIS Test Pattern tool and `^/[a-z]+/?$` matches `/about/` or `/about` but not `/css/main.css` – cheesemacfly Feb 25 '13 at 16:10

0 Answers0