Dino Geek, try to help you

How to configure URL rewriting with mod_rewrite?


URL rewriting with `mod_rewrite` in Apache HTTP Server allows you to manipulate URLs to be more user-friendly, SEO-friendly, and to enable smooth redirections. By using `mod_rewrite`, you can transform an ugly URL like `http://example.com/index.php?page=something` into a cleaner, easier-to-read URL like `http://example.com/something`.

To configure URL rewriting, you first need to ensure that the `mod_rewrite` module is enabled. Here is a step-by-step guide, complete with examples and sources for further reference:

  1. Step 1: Enable `mod_rewrite`
    1. Check if `mod_rewrite` is already enabled: – Run the command `apachectl -M | grep rewrite` on your server. If you see `rewrite_module`, it’s already enabled.

1. Enable `mod_rewrite` if it’s not already enabled: – Run the command: `sudo a2enmod rewrite` – Restart Apache to apply the changes: `sudo systemctl restart apache2`

  1. Step 2: Update the Apache Configuration
    1. Allow `.htaccess` files to override Apache settings: – Open the Apache configuration file, often located at `/etc/apache2/sites-available/000-default.conf` or `/etc/httpd/conf/httpd.conf`. – Locate the `` directive for your web root. For example: \`\`\`apache Options Indexes FollowSymLinks AllowOverride None Require all granted \`\`\` – Change `AllowOverride None` to `AllowOverride All` to permit the use of `.htaccess` files: \`\`\`apache Options Indexes FollowSymLinks AllowOverride All Require all granted \`\`\`

– Save the file and restart Apache: `sudo systemctl restart apache2`

  1. Step 3: Create and Configure the `.htaccess` File
    1. Create a `.htaccess` file in your web root directory: – Navigate to the web root directory: `cd /var/www/html` – Create or edit the `.htaccess` file: `nano .htaccess`

1. Add rewrite rules: – Here’s a basic example that rewrites a request for `http://example.com/something` to `http://example.com/index.php?page=something`: \`\`\`apache RewriteEngine On RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 [L] \`\`\`

1. Explanation of the above rewrite rule: – `RewriteEngine On`: Enables the `mod_rewrite` engine. – `RewriteRule`: The core directive to define a rewrite rule. – `^([a-zA-Z0-9]+)$`: This pattern will match any single word consisting of letters and numbers. – `index.php?page=$1`: The target URL that includes the captured group as a query parameter. – `[L]`: The flag to indicate that this is the last rule and no further rewriting should be done.

  1. Examples of Common Rewrite Rules
    1. Removing `.php` extension: \`\`\`apache RewriteEngine On RewriteCond %{REQUEST\_FILENAME} !-f RewriteRule ([.]+)$ $1.php [NC,L] \`\`\` – This will rewrite `http://example.com/filename` to `http://example.com/filename.php`.

1. Redirect from `http` to `https`: \`\`\`apache RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.\*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] \`\`\` – Forces any request over `http` to be redirected to `https`.

1. Removing `www` from URLs: \`\`\`apache RewriteEngine On RewriteCond %{HTTP\_HOST} ^www.(.\*)$ [NC] RewriteRule ^(.\*)$ http://%1/$1 [R=301,L] \`\`\` – Redirects `http://www.example.com` to `http://example.com`.

  1. Sources
    - [Apache HTTP Server Documentation](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html): Comprehensive guide on `mod_rewrite`.
    - [DigitalOcean Tutorial on Mod_Rewrite](https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite): Detailed instructions on setting up `mod_rewrite`.
    - [Stack Overflow](https://stackoverflow.com/questions/tagged/mod-rewrite): Various questions and answers related to `mod_rewrite`.

By following these steps and examples, you can effectively configure URL rewriting with `mod_rewrite`, making your URLs cleaner, more user-friendly, and improving overall site navigation.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use