[SalesForce] How to override the edit link in relatedlist to redirect it to standard edit page of the record

I need to override the edit link of a relatedlist with visualforce page. The visualforce page will do nothing but redirect the user to the standard edit page of the record. I tried the following but it keeps on performing the action again and again.

<apex:page standardController="Escrow_Contact__c" extensions="PWEscrowContactController" action="{!redirectToPage}">
</apex:page>

Controller:

public class PWEscrowContactController {

private final Escrow_Contact__c contact1 {get;set;}

public PWEscrowContactController(ApexPages.StandardController stdcontroller){
  this.contact1 = (Escrow_Contact__c)stdController.getRecord();  
}

 public PageReference redirectToPage() {
     return new ApexPages.StandardController(contact1).edit();
 }

}

I need to accomplish this because I have used a relatedlist within Visualforce page and used the page in standard layout as a section and while performing edit within this Visualforce page it redirects me directly to the Visualforce page instead of redirecting me to the parent record page.

The url when edit link is clicked from standard related list layout is,
https://cs13.salesforce.com/a0aW0000001S6nB/e?retURL=%2Fa01U000000eZke5

but the url when edit link is clicked from the Visualforce page section of related list layout is,
https://cs13.salesforce.com/a0aW0000001S6nB/e?retURL=%2Fapex%2FvfEscrowContacts%3Finline%3D1%26id%3Da01U000000eZke5

I need to overcome this issue.

Best Answer

If you want to override the Edit action for a specific object you need to go to the object and select 'Buttons, Links, and Actions'.

After that, you can override any action on that page. If you click on 'Edit' on the left side of the Edit label you will see the options for that action. Just change the 'Override with' to 'Visualforce Page' and select the page you want to use from the drop-down options.

To access the object if Standard: Setup - Build - Customize

To access the object if Custom: Setup - Create - Objects

Hope this helps.

UPDATE: It is also redundant after clicking on Edit to send the user to Edit again, that will be a loop. You are redirecting the user to the same action on your class. Basically, you are sending the user over and over to the same Edit page that is firing the Edit page.

Also consider that if you override the Edit action on the object, that will affect to all your Edit actions for that object. It will become your main Edit operation, not only for the Edit link on the view list.

And there is an </apex:apexpage> missing on your Visualforce page