How can I rewrite seo friendly url on struts?

by annabell_mcdermott , in category: SEO , 6 months ago

How can I rewrite seo friendly url on struts?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by cameron_walter , 6 months ago

@annabell_mcdermott 

To rewrite SEO friendly URLs on Struts, you can follow these steps:

  1. Define a new action mapping in struts-config.xml for the SEO friendly URL. For example, if you want to rewrite "example.com/products/123" to "example.com/productDetails.do?id=123", add the following action mapping:
1
2
3
<action path="/products/{id}" type="com.example.ProductDetailsAction" parameter="id">
  <forward name="success" path="/productDetails.do"/>
</action>


  1. Create a new action class (e.g., ProductDetailsAction) that will handle the request for the rewritten URL. This class should extend the Action class and implement the execute method.
1
2
3
4
5
6
7
8
9
public class ProductDetailsAction extends Action {
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    String id = request.getParameter("id");
    // Process the request and retrieve the product details based on the ID
    // Set the retrieved product details in the request attributes or form
    // Forward to the appropriate JSP or another action
    return mapping.findForward("success");
  }
}


  1. Update the JSP or HTML pages to use the SEO friendly URLs instead of the original URLs. Replace links like Product Details with Product Details.
  2. Optionally, you can provide a link to redirect the old URL format to the new SEO friendly URL format. For example, if someone accesses the old URL "example.com/productDetails.do?id=123", you can redirect them to the new URL "example.com/products/123" using a 301 redirect.


Remember to update your web server configuration (e.g., Apache) to rewrite the incoming SEO friendly URLs to the appropriate servlet or action URL. This can be achieved using Apache's mod_rewrite module or similar URL rewriting mechanisms depending on your web server.