How to use mod_rewrite in .htaccess?

by genevieve_boehm , in category: SEO , 9 months ago

How to use mod_rewrite in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by priscilla.langworth , 9 months ago


To use mod_rewrite in .htaccess, follow these steps:

  1. Create or edit the .htaccess file in the root directory of your website.
  2. Enable the mod_rewrite module by adding the following line at the beginning of the .htaccess file: RewriteEngine On
  3. Write your rewrite rules using the RewriteRule directive. Here is the basic syntax: RewriteRule pattern substitution [flags] pattern: This is the regular expression pattern that matches the URL you want to rewrite. substitution: This is the replacement URL or file path that you want to rewrite the matched URL to. flags (optional): These are additional flags that can modify the behavior of the rewrite rule. Some commonly used flags include [L] for last rule, [NC] for case-insensitive matching, and [R] for redirect. For example, to redirect all requests for "" to "", you can use the following rule: RewriteRule ^old-page$ new-page [R=301,L] This rule matches the exact string "old-page" and redirects it to "new-page" with a 301 (permanent) redirect.
  4. Test your rewrite rules to ensure they work as intended. Make sure to clear your browser cache before testing.

Remember to proceed with caution when using mod_rewrite, as incorrect or conflicting rewrite rules can lead to unexpected behavior or errors.