Cannot get Rewrite to Work

ISAPI_Rewrite is a powerful URL manipulation engine based on regular expressions
User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Cannot get Rewrite to Work

09 Aug 2012, 10:14

Hello,

Please help! I am pretty new to using ISAPI_Rewrite and am still getting my barrings.

I have my website linked up so that it will take whatever the url is and find the corresponding XML and XSL based on the link. For example if the website link was "ThisIsMyLocalHost/MySite/sub_site", sub_site.xml and sub_site.xsl would be used.

My .ini file looks as follows:

Code: Select all
[ISAPI_Rewrite]
RewriteRule /MySite/sub_site/.* /MySite/ [I,RP]


When I click the button that should be showing the URL path "ThisIsMyLocalHost/MySite/sub_site". It just loads the page "ThisIsMyLocalHost/MySite/" (Which is the first page loaded). The correct URL path shows up in the browser (ThisIsMyLocalHost/MySite/sub_site), but the XML and XSL does not load. I feel like when the page is loading, there is no information on what the original URL was, but I do not really know where to go from here.

Any help would be much appreciated. Thank you!

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

09 Aug 2012, 11:08

Hello,

"I have my website linked up so that it will take whatever the url is and find the corresponding XML and XSL based on the link. For example if the website link was "ThisIsMyLocalHost/MySite/sub_site", sub_site.xml and sub_site.xsl would be used."
- thanks for your explanation, but it's still unclear what should happen upon request to, for example, ThisIsMyLocalHost/MySite/sub_site...
With the current rule it will just redirect to ThisIsMyLocalHost/MySite/ - is that what you expect? If not, please provide some more explanation with examples.

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

09 Aug 2012, 12:34

Sorry, I will try to explain a little better.

What I believe that I want to happen is have all URL paths redirect to ThisIsMyLocalHost/MySite/.

So that Sites like:
ThisIsMyLocalHost/MySite/sub_site/yet_another_webpage1
ThisIsMyLocalHost/MySite/sub_site/yet_another_webpage2
ThisIsMyLocalHost/MySite/sub_site/yet_another_webpage3

would all link back to ThisIsMyLocalHost/MySite/. And then the code would pick up the URL, strip the host, and then find the correct XML and XSL to load in the page with the corresponding URL:

yet_another_webpage1.xml
yet_another_webpage1.xsl

yet_another_webpage2.xml
yet_another_webpage2.xsl

yet_another_webpage3.xml
yet_another_webpage3.xsl

But it does not seem to be picking up the URL path: ThisIsMyLocalHost/MySite/sub_site, instead it just seems to be picking up ThisIsMyLocalHost/MySite/ and displaying that XML and XSL.

Again, the idea is to really only have one site. So when a button with the link: ThisIsMyLocalHost/MySite/sub_site, is clicked on the website it shows in the address (navigation) bar, and then shows the corresponding XML and XSL for that path.

I know that it has something to do with my Rewrite code. I am just unsure how to get the code to show ThisIsMyLocalHost/MySite/sub_site. It seems to show in the address (navigation) bar like this: ThisIsMyLocalHost/MySite/sub_site. But like i said, It seems to be picking up ThisIsMyLocalHost/MySite/ instead.

I am sorry if this is still unclear. Like I said before, I am pretty new at this and it is difficult to explain. Please let me know and I will try to explain again.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

10 Aug 2012, 03:59

Hello,

Thanks for a more detailed explanation. I believe it's clearer now.

Please try to use the following config

[ISAPI_Rewrite]
RewriteRule /MySite/sub_site/.* /MySite/ [I,L]

and in your code you can use HTTP_X_REWRITE_URL variable to get the initially requested URL and load corresponding xml and xsl files.

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

10 Aug 2012, 09:23

Thank you for getting back to me.

I made the changes that you recommended and still have the same issues. In my code I have included the HTTP_X_REWRITE_URL variable as well.

Can you think of anything else that may be the issue?

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

10 Aug 2012, 10:37

Hello,

Could you please tell, when you had your initial rule in place,

[ISAPI_Rewrite]
RewriteRule /MySite/sub_site/.* /MySite/ [I,RP]

did the URL in the address bar change to ThisIsMyLocalHost/MySite/?

Also, please confirm you are using ISAPI_Rewrite 2, not 3.

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

10 Aug 2012, 10:50

Hello,

No, the URL in the address bar displayed ThisIsMyLocalHost/MySite/sub_site/ when I had the initial rule in place. It also stayed the same when i changed it to [I,L].

I am using ISAPI_Rewrite 2.

Thank you.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

10 Aug 2012, 11:28

That is strange... RP flag should perform a redirect and the URL in the address bar should change.
Let's try another rule just for testing:

[ISAPI_Rewrite]
RewriteRule /MySite/sub_site/.* http\://www.google.com [I,RP]

Make the same request you did before and report what result you get.

Also, please tell what status IIS log writes for this request.

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

10 Aug 2012, 11:45

I changed the Rewrite as requested and had no luck. The address bar changed to ThisIsMyLocalHost/MySite/sub_site/ but it still displayed ThisIsMyLocalHost/MySite/.

The status in the IIS log is "loaded" and the priority says "high".

If I add:

RewriteRule /MySite/sub_site/.* http\://www.google.com [I,RP]

then type ThisIsMyLocalHost/MySite/sub_site/ into the address bar it reroutes to Google, but if i try and click on the button it does not work. I am working in chrome and checked the Network tab in the debugger. It seems that when i click the link it shows that i want to go to www.google.com but status says (canceled).

Thank you.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

10 Aug 2012, 12:48

Hello,

I meant the status code, e.g. 200 or 301.
Please check it once again, 'cause it's important to know the status code for both rules:

RewriteRule /MySite/sub_site/.* /MySite/ [I,RP]
and
RewriteRule /MySite/sub_site/.* http\://www.google.com [I,RP]

Also, please make sure you don't have any other rules (e.g. in global httpd.ini) that may interfere with the current rule.

And can you show the code you use for the button.

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

10 Aug 2012, 13:45

Hello,

Sorry for the confusion.

RewriteRule /MySite/sub_site/.* /MySite/ [I,RP] - showed a 301 code.
RewriteRule /MySite/sub_site/.* http\://www.google.com [I,RP] - showed a 301 code and then showed (canceled).

I do not have any other rules in the code.

The following is the code for my button:

XSL:
Code: Select all
<a>
        <xsl:attribute name="href">
              <xsl:value-of select="link"/>
        </xsl:attribute>
        <xsl:value-of select="name"/>
        <xsl:if test="child">
              <span class="arrow_down"></span>
        </xsl:if>
</a>


XML:
Code: Select all
<parent>
    <name>Sub Site</name>
    <link>/MySite/sub_site/</link>
</parent>


Hope this helps! Thank you!

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

14 Aug 2012, 05:41

Hello,

sorry to make you wait.
I guess I need some more clarification:
- where are the xml/xsl files located physically?
- what is the specific request you are trying to make working?

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

14 Aug 2012, 09:14

Hello,

That is okay.

the physical path of the files are as follows:
/MySite/xml/sub_site.xml - XML
/MySite/xsl/sub_site.xsl - XSL

I am not quite sure about what you mean when you say "what is the specific request you are trying to make work". Could you elaborate more please?

Thank you!

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

14 Aug 2012, 10:30

"I am not quite sure about what you mean when you say "what is the specific request you are trying to make work". Could you elaborate more please?"
- I mean upon what action should the target rule fire? Is it a click on a certain button? What is the URL requested?
Can you give a link to a real page where this button is located? If it's private, you can send it to me by email: [email protected].

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

14 Aug 2012, 10:47

It is a click function on a certain button.

We are using jQuery Mobile and it is a list-divider set. So, in theory, each button would go to a link. I do not have a website to show you because it is not up and running (still in production). Right now we have it coming from a local host.

The link path on the button is /MySite/sub_site/. When I run the website the link shows ThisIsMyLocalHost/MySite/sub_site/. I hope this helps.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

15 Aug 2012, 04:47

Ok, and when you click the button you want this:

/MySite/xml/sub_site.xml
and
/MySite/xsl/sub_site.xsl

right?

How do you achieve this in you code? Can you show the corresponding excerpt?

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

15 Aug 2012, 09:18

That is correct. Those would be the paths that we would want.

This is the following code that you asked for:
Code: Select all
public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        String CurrentHost = Request.ServerVariables["HTTP_HOST"];
        String rewriteUrl = Request.ServerVariables["HTTP_X_REWRITE_URL"];
       

        try
        {   
            if (rewriteUrl != "/MySite/")
            {               
                // trim out the host name from the url so we are only left with the sub section.
                var locationName = rewriteUrl.Replace("/MySite/", String.Empty);
                base.ContentXmlPath = this.BuildXmlPath(locationName);
                base.ContentXslPath = this.BuildXslPath(locationName);
            }

            base.LoadBodyContent(content);
        }
        catch (Exception ex)
        {
            // ToDo: Add exception logging.
            Response.Redirect("error.aspx");
        }
    }

    private string BuildXslPath(string path)
    {
        return String.Format("xsl/{0}.xsl", path);
    }

    private string BuildXmlPath(string path)
    {
        return String.Format("xml/{0}.xml", path);
    }   
}


Thank you.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

15 Aug 2012, 10:07

Hello,

Thanks for the details.
Now could you please show what gets into 'rewriteUrl' variable after the request.
Also, please enable rewrite logging in httpd.conf by putting

RewriteLogLevel 9

And show the records you get in rewrite.log for your request (/MySite/sub_site)

User avatar
Posts: 12
Joined: 09 Aug 2012, 09:51

Re: Cannot get Rewrite to Work

15 Aug 2012, 11:21

Hello,

rewriteUrl shows "/MySite/" after the request.

I am sorry, but I do not understand exactly what you would like me to do with the "RewriteLogLevel 9". Could you please elaborate more?

Thank you!

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Cannot get Rewrite to Work

16 Aug 2012, 05:41

Hello,

Sorry, I've confused the version of ISAPI_Rewrite you have, so no need to put 'RewriteLogLvel 9'.

However, rewriteUrl variable doesn't contain the value it's supposed to store, I believe it should contain 'MySite/sub_site'.
One of the reasons for that may be double registration of ISAPI_Rewrite. Please make sure that ISAPI_Rewrite filter is registered only globally in IIS -> Web Sites properties -> ISAPI filters tab, BUT NOT for the site IIS -> Web Site properties -> ISAPI filters tab.

Also please specify what IIS version you are using.

Next

Return to ISAPI_Rewrite 2.x

Who is online

Users browsing this forum: No registered users and 12 guests