Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up redirect in web.config file

I'm trying to redirect some unfriendly urls with more descriptive ones. These urls end in .aspx?cid=3916 with the last digits being different for each category name page. I want it to instead redirect to Category/CategoryName/3916. I tried this in the web.config file:

<location path="Category.aspx?cid=3916"> <system.webServer>   <httpRedirect enabled="true" destination="http://www.site.com/Category/CategoryName/3916" httpResponseStatus="Permanent" /> </system.webServer> 

but since it didn't end with just the extension, it didn't work. Is there an easy way to get this to work? I'm using IIS 7.5.

like image 664
Pear Berry Avatar asked May 01 '12 15:05

Pear Berry


People also ask

How do I set up a web redirect?

Redirects allow you to forward the visitors of a specific URL to another page of your website. In Site Tools, you can add redirects by going to Domain > Redirects. Choose the desired domain, fill in the URL you want to redirect to another and add the URL of the new page destination. When ready, click Create.

How do I redirect www to non www in web config?

Use IIS rewrite rule to redirect (301) all www requests to non-www. Code first, talks later. Replace the “yourdomain” with your domain name and add it under the system. webServer section in the Web.

How do I setup a redirect in IIS?

In IIS Manager, expand the local computer, right-click the Web site or directory you want to redirect, and click Properties. Click the Home Directory, Virtual Directory, or Directory tab. Under The content for this source should come from, click A redirection to a URL.


2 Answers

  1. Open web.config in the directory where the old pages reside
  2. Then add code for the old location path and new destination as follows:

    <configuration>   <location path="services.htm">     <system.webServer>       <httpRedirect enabled="true" destination="http://domain.com/services" httpResponseStatus="Permanent" />     </system.webServer>   </location>   <location path="products.htm">     <system.webServer>       <httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" />     </system.webServer>   </location> </configuration> 

You may add as many location paths as necessary.

like image 166
MUG4N Avatar answered Sep 23 '22 04:09

MUG4N


You probably want to look at something like URL Rewrite to rewrite URLs to more user friendly ones rather than using a simple httpRedirect. You could then make a rule like this:

<system.webServer>   <rewrite>     <rules>       <rule name="Rewrite to Category">         <match url="^Category/([_0-9a-z-]+)/([_0-9a-z-]+)" />         <action type="Rewrite" url="category.aspx?cid={R:2}" />       </rule>     </rules>   </rewrite> </system.webServer> 
like image 26
vcsjones Avatar answered Sep 20 '22 04:09

vcsjones