How to make long urls friendly with .htaccess?

by laverna_hirthe , in category: SEO , 4 months ago

How to make long urls friendly with .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by virginie , 4 months ago

@laverna_hirthe 

To make long URLs friendly with .htaccess, you can use a technique called URL rewriting. Here are the steps to achieve this:

  1. Enable the RewriteEngine in your .htaccess file by adding the following line at the top: RewriteEngine On
  2. Define your rewrite rules using the RewriteRule directive. The general syntax is: RewriteRule pattern target [flags] The pattern is a regular expression that matches the URL you want to rewrite. The target is the destination URL you want to redirect to. You can use flags to modify the behavior of the rewrite rule.
  3. For example, if you have a long URL like "example.com/page.php?id=123&var=value" and you want to rewrite it to "example.com/page/123/value", you can use the following rule: RewriteRule ^page/([^/]+)/([^/]+)$ page.php?id=$1&var=$2 [L] The ^ character denotes the start of the URL path. The $1 and $2 in the target represent the captured groups from the pattern. The [L] flag indicates that this is the last rule to process if it matches.
  4. Save the .htaccess file and upload it to your website's root directory. Make sure the file is named ".htaccess" (note the leading dot).


After following these steps, your long URL should now be accessible using the friendly URL you defined in the rewrite rule.