简体   繁体   中英

How can I correctly rewrite a URL with .aspx to a URL without .aspx?

I have a site that currently uses the .aspx extension on its pages. It's getting a Joomla conversion and the .aspx extension will not work anymore. I need it so that if someone enters the .aspx extension, it will just get removed the URL so none of the SEO on the current site breaks.

For example, I need www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx to be rewritten/redirected to www.arrc.com.php5-17.websitetestlink.com/services/managed-services

This is what I have in my web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
     <rules>
      <rule name="Migrate to PHP">
       <match url="^([_0-9a-z-]+)\.aspx" />
       <action type="Redirect" redirectType="Permanent" url="{R:1}" />
      </rule>
      <rule name="Rewrite .aspx">
         <match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
         <action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
      </rule>
     </rules>
    </rewrite>
  </system.webServer>
</configuration>

The first URL match is for any URLs that have a URL like services.aspx instead of services/managed-services.aspx

Whenever I go to www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx it gives me an internal server error, but www.arrc.com.php5-17.websitetestlink.com/services.aspx rewrites correctly. What can I do to fix this?

They are greedy, switch the order.

   <rule name="Rewrite .aspx">
     <match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
     <action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
  </rule>

 <rule name="Migrate to PHP">
   <match url="^([_0-9a-z-]+)\.aspx" />
   <action type="Redirect" redirectType="Permanent" url="{R:1}" />
  </rule>

Two potential issues:

  1. I think for rewriting, it's going to hit them in order, which means the first rule is going to always kick off. Try switching the order of the rule.

  2. The dash should work correctly in [_0-9a-z-], but try escaping the dash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM