topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday April 16, 2024, 12:42 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: URL Rewrite Problem  (Read 6631 times)

BGM

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 562
    • View Profile
    • bgmCoder DC
    • Read more about this member.
    • Donate to Member
URL Rewrite Problem
« on: June 19, 2014, 09:07 AM »
Hello, folks.

I need some help with a problem on my webserver involving url-rewrite and redirection in my IIS7.  I made a post at ServerFault, but noone is hardly even viewing it.

Here is the contents of my post there:

We have our own domain and have several subdomains. One of the sites needs to be accessed only via SSL, but people are allowed to type http and still arrive there. The URL-Rewrite forces the http into https.

So my two subdomains are: sub1.domain.org and sub2.domain.org.

Thus, if I type

Code: Text [Select]
  1. http://sub1.domain.org/page_on_domain1

it is rewritten to

Code: Text [Select]
  1. https://sub1.domain.org/page_on_domain1

-which is correct. But if I do this (which is not possible - page_on_domain1 does not exist on sub2.domain.org:

Code: Text [Select]
  1. https://sub2.domain.org/page_on_domain1

My url-rewrite rule seems to work properly, but I want that last address to give me an error page instead, since the page doesn't exist on that subdomain.

I am testing this in Firefox on Windows 7 x64 Pro.

My url-rewrite rule is as follows (I am doing it through the IIS7 GUI):

Code: Text [Select]
  1. Requested URL:  Matches the Pattern:  (*)  Using Regular Expressions (ignore case)
  2. Conditions:  Match All:  Input:  {HTTPS}  Matches the Pattern:  ^Off$
  3. Redirect URL:  https://sub1.domain.org/{REQUEST_URI}

Now, I've tried changing the Requested URL Pattern to: .*sub1.domain.org/.* but the regex tester in IIS7 says it doesn't match against https://sub1.domain.org - I don't understand that.

How can I keep the rewrite of http to https for sub1.domain.org, but not have any effect at all on sub2.domain.org ??

I've been stuck on this for a long time.  This is my first ever post to the Living Room.
Thank you!
« Last Edit: June 19, 2014, 01:30 PM by BGM »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: URL Rewrite Problem
« Reply #1 on: June 19, 2014, 01:24 PM »
To get more traction, I'd suggest copying the text of your question here, in addition to linking to it.

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: URL Rewrite Problem
« Reply #2 on: June 21, 2014, 08:21 AM »
URL rewriting is a part of the DNN framework. You can likely look into that and just rip it out and use it.

They key to URL rewriting in IIS 7+ is making it happen at the application layer. Once you've got it there, you're golden.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

BGM

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 562
    • View Profile
    • bgmCoder DC
    • Read more about this member.
    • Donate to Member
Re: URL Rewrite Problem
« Reply #3 on: June 21, 2014, 08:53 AM »
@Renegade,  I have a url-rewrite module already installed and working in my IIS.  I can't figure out how to get the settings and regex right.

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: URL Rewrite Problem
« Reply #4 on: June 21, 2014, 09:57 AM »
@Renegade,  I have a url-rewrite module already installed and working in my IIS.  I can't figure out how to get the settings and regex right.

I don't know your system there, but it looks like a simple conditional will work to solve that.

Code: C# [Select]
  1. if (condition)
  2. {
  3.         // send to https
  4. } else {
  5.         // send to http
  6. }

Can you put that in the global.asax file? Or wherever the regex logic is?

There are some very good, robust code bases out there that can help. DNN might be a good source to look at.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

BGM

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 562
    • View Profile
    • bgmCoder DC
    • Read more about this member.
    • Donate to Member
Re: URL Rewrite Problem
« Reply #5 on: June 21, 2014, 10:10 AM »
Thanks for your help. 

However, this isn't a coding issue - I'm not writing this into the aspx code.  The System is IIS7, and I am using the webserver's GUI to rewrite the URL for incoming http requests.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: URL Rewrite Problem
« Reply #6 on: June 21, 2014, 10:52 AM »
Probably the reason you're not getting views, etc, is that it's not a URL-rewrite issue either; it's a regex issue.  The . character is a regex character, so you have to escape it.  Also, you should probably make the / optional.  I put some regex below.

Code: Text [Select]
  1. .*sub1\.domain\.org/*.*

In the future, you might want to run your regex through a tester to make sure it's doing what you want it to do.  There are several online.  RegexHero is one, but it uses the Silverlight parser, so if you're typically doing perl rather than .net expressions, you'll want to find another.  I use RegexBuddy... but it's a paid app.  Very much worth it to save sanity.

BGM

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 562
    • View Profile
    • bgmCoder DC
    • Read more about this member.
    • Donate to Member
Re: URL Rewrite Problem
« Reply #7 on: June 21, 2014, 11:22 AM »
Ah, the regex did, indeed help - thank you very much.  I tried with some escapes but was having troubles.
Now it rewrites to the site correctly, whether being http or https.


However, I still have a problem.
If I type https://sub2.domain/page_from_sub1
it rewrites to
https://sub1.domain/page_from_sub1

It is supposed to get my my error page because page_from_sub1 does not exist on sub2.
How do I fix that?

The regex is correct. http://sub2.domain never matches the regex.  So why is it getting rewritten?