How can I display SEO-friendly urls using mod_rewrite?

by domenico.weimann , in category: SEO , a year ago

How can I display SEO-friendly urls using mod_rewrite?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

2 answers

Member

by pietro , a year ago

@domenico.weimann 

To display SEO-friendly URLs using mod_rewrite, you can write rewrite rules in the .htaccess file in your website's root directory. The rules should map the desired, "friendly" URL to the actual, "unfriendly" URL used by your website. Here's a simple example:

1
2
RewriteEngine on
RewriteRule ^products/([0-9]+)/([a-zA-Z0-9_-]+)$ product.php?id=$1&name=$2 [NC,L]


This rule will rewrite URLs like http://example.com/products/123/my-product-name to http://example.com/product.php?id=123&name=my-product-name. The [NC,L] flags are used to make the pattern case-insensitive and to stop the processing of additional rewrite rules.


Note that mod_rewrite requires the Apache web server and it must be enabled on your server. You may need to consult your web hosting provider or server administrator to confirm that mod_rewrite is available and enabled.

by declan_ritchie , 3 months ago

@domenico.weimann 

To display SEO-friendly URLs using mod_rewrite, you need to follow these steps:

  1. Enable mod_rewrite: Ensure that mod_rewrite is enabled on your Apache server. You can check this by looking for the "mod_rewrite" module in the Apache configuration or by creating a file called "info.php" with the following contents:
1
<?php phpinfo(); ?>


Visit this file in your browser (e.g., http://yourwebsite.com/info.php) and search for "mod_rewrite" to see if it's enabled.

  1. Create a .htaccess file: In your website's root directory, create a file called ".htaccess" if it doesn't already exist.
  2. Enable RewriteEngine: Add the following line to the beginning of your .htaccess file to enable the RewriteEngine:
1
RewriteEngine On


  1. Write rewrite rules: Below the RewriteEngine line, you can write the rewrite rules using the RewriteRule directive. Here's an example to rewrite a URL with parameters to a more friendly URL:
1
RewriteRule ^products/([0-9]+)/([a-zA-Z0-9_-]+)$ product.php?id=$1&name=$2 [NC,L]


This rule will rewrite a URL like http://example.com/products/123/my-product-name to http://example.com/product.php?id=123&name=my-product-name. The regular expression pattern ^products/([0-9]+)/([a-zA-Z0-9_-]+)$ matches the friendly URL format and captures the parameters.

  1. Flags: The [NC,L] flags are optional and can be appended to the end of the RewriteRule. NC makes the pattern case-insensitive, while L stops the processing of additional rewrite rules if this rule matches.


Note that these are basic examples, and you can customize the rules to fit your specific needs.

  1. Test: Save the .htaccess file and visit your newly created SEO-friendly URL to test if it's properly redirected to the actual URL. Make sure the original URL with parameters still works as expected.


It's important to be careful while working with mod_rewrite rules as they can impact your website's functionality if not implemented correctly. Take backups of your .htaccess file before making any changes and test thoroughly to ensure everything is working as expected.