2

i am currently using IIS7's Url Re-write module, but the main loophole of using the IIS7's url re-write module, is that i have to write rule for all the page,which i want to use on the website, i want to use a comman rule and redirect it to particular page (say homepage) and using global.asax i can redirect it to desired page...

Is it possible with url re-write or is there any tool available i can use for this purpose, or a code sample that could help me doing this.

i dont want extension in the url.

i have pages like index.aspx, news.aspx, artists.aspx, lessons.aspx... i want the urls like index, news, lessons, artists, i created a rule in web.config like

< rewrite>  
 < rules>  
   < rule name="urlType1">  
     < match url="^(\w*)" />  
     < action type="Rewrite" url="default.aspx" appendQueryString="false" />  
   < /rule>  
 < /rules>  
< /rewrite>  

this will land any page to default.aspx, and then using rawUrl in the global.asax, i am checking for the page like if user has entered "news" then i rewrite to news.aspx

Hope this has helped.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Abbas
  • 4,948
  • 31
  • 95
  • 161

2 Answers2

1

You can do just as you're suggesting in your question--redirect all requests to a single URL: And then in your Global.asax you could call Server.Transfer( "~/file1.aspx" ) to forward the request to a particular file.

Or, you could transfer directly from your URL Rewrite rule and skip further processing in your Global.asax file. For instance, this rule will read the incoming URL that has no file extention, and then forward the request on to a file that has a file extension:

        <rule name="Append a file extension to all requests discard querystring" stopProcessing="true">
            <match url="^(.*)\?" />
            <action type="Rewrite" url="{R:1}.aspx" />
        </rule>

Read more about URL Rewriting rules on Ruslan Yakushev's blog at http://ruslany.net/.

Geoffrey McGrath
  • 1,663
  • 1
  • 14
  • 35
-1

[EDIT] Okay, so for what you are asking...as far as I know, you do need to create a specific rewrite rule for each page. I was thinking more along the lines of MVC, where when you go to:

/news

it routes to

/default.aspx

which calls the NewsController.Index and displays the news page from default.aspx. However, to actually break everything out into individual pages and just trying to remove the extension...as far as I know, you have to create a new rule for each instance.

Redirect rules can be configured from the web.config files.

E.g. Here's what WordPress does in the web.config file that's included with WordPress:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
                <rule name="wordpress" patternSyntax="Wildcard">
                    <match url="*"/>
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                        </conditions>
                    <action type="Rewrite" url="index.php"/>
                </rule></rules>
    </rewrite>
  </system.webServer>
</configuration>

The routes ALL traffic to the index.php page. The index.php file then reads what the URL is and spits out data based on the URL. It doesn't redirect it to a different page after the redirect, it--rather--decides what content to display.

MVC works along the lines of, you see this url:

/news         > will call > NewsController.Index();
/news/index   > will call > NewsController.Index();
/news/view    > will call > NewsController.View();
/news/read/id > will call > NewsController.Read(id);

These Controllers, then typically get data from the database and apply the data to a "View" (html page that sits somewhere on the server with variables to display the data the controller passes to it).

Kevin Nelson
  • 7,613
  • 4
  • 31
  • 42
  • thanks for your reply, i have updated my question, please have a look – Abbas Jun 08 '11 at 18:30
  • edited post. Unfortunately, don't know of a way to do your exact configuration without individual rewrites. – Kevin Nelson Jun 08 '11 at 18:59
  • can you elaborate what you were asking for MVC, which routes to default.aspx and calls NewsController.Index and displays the news page on default.aspx – Abbas Jun 08 '11 at 19:01
  • or do you have any idea of adding rewrite rule at runtime in the same format above in the seperate file and attach that file in web.config file – Abbas Jun 08 '11 at 19:05
  • added explanation about MVC as much as I can, with an example of a redirect of all pages to index. I have no knowledge of how to make rules at runtime if it's possible or even advisable. – Kevin Nelson Jun 08 '11 at 19:22