How can I display seo friendly urls using mod_rewrite?

by jose_gulgowski , in category: SEO , 4 months ago

How can I display seo friendly urls using mod_rewrite?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by emelie , 4 months ago

@jose_gulgowski 

To display SEO-friendly URLs using mod_rewrite, you can follow the steps below:

  1. Ensure that mod_rewrite module is enabled on your web server.
  2. Create or edit your website's .htaccess file in the root directory.
  3. Start by enabling the rewrite engine by adding the following line at the beginning of the .htaccess file: RewriteEngine On
  4. Use the RewriteRule directive to define the pattern of the URL you want to rewrite and the corresponding destination URL. For example, if you want to rewrite example.com/page.php?id=123 to example.com/page/123, use the following rule: RewriteRule ^page/([0-9]+)$ page.php?id=$1 [L] This rule matches the pattern using regular expressions (^page/([0-9]+)$) and redirects it to the destination URL (page.php?id=$1). [L] indicates that it's the last rule to apply if the pattern matches.
  5. If you have multiple rules, separate them with [L] to ensure only one rule is applied per request.
  6. Save the .htaccess file and upload it to your website's root directory.


Upon implementing these steps, when a visitor accesses example.com/page/123, the web server will internally handle the request as example.com/page.php?id=123. However, the visitor will see and interact with the SEO-friendly URL.


Remember to test each rule thoroughly and ensure it doesn't conflict with other rules or cause unexpected behavior.