Development and databases

Set up friendly URLs and mod_rewrite rules

Reviewed on 3 min read mod rewrite htaccess

mod_rewrite rules let you route a URL to another resource or return a redirect to the browser. At Hostsuar, LiteSpeed interprets Apache-compatible rules. The application decides which routes it needs and how they should be resolved.

Rewrite and redirect

An internal rewrite can serve index.php when you visit /productos, keeping that URL in the browser. An HTTP redirect returns a destination and the browser requests another address. They are not interchangeable: adding a redirect does not configure a CMS's internal routes.

This line enables rule processing in the relevant context:

RewriteEngine On

On its own it does not create friendly URLs. It needs the application's specific rules.

Apply the project's rules

  1. Confirm the domain's document root and keep a private copy of the existing .htaccess.
  2. Get the rules from the application's documentation for your type of installation, including whether it is in a subfolder or is a multisite.
  3. If it provides a file such as htaccess.txt, review its content before renaming it. If .htaccess already exists, also keep the rules the hosting or the rest of the site needs.
  4. Turn on the application's friendly URLs or permalinks option where applicable.
  5. Test an internal page, a static file and an address that does not exist.

In WordPress, Settings > Permalinks can regenerate its rules if it has permission to write the file; otherwise it shows instructions for applying them. Do not replace a multisite network's rules with those of a single installation.

Check the result

  • If the home page works but internal pages return 404, check where the rules are, the domain's document root and the application's route settings.
  • If there is a loop, check the HTTPS, www and subfolder rules and the redirects set up in the application or the proxy.
  • If a 500 appears, revert the change and check the log: there may be incorrect syntax or a directive that is not allowed.

In .htaccess, the RewriteRule pattern is evaluated relative to that directory and does not normally start with /. A rule copied from the server's main configuration may need adapting. See the official introduction to mod_rewrite when you write your own rules.

Keep old URLs that are still in use and set up redirects to their equivalent if you change the structure. Avoid sending them all to the home page: each destination should match the content the visitor was looking for.

You may also find useful

Something wrong or out of date? Tell us and we will review it.