@hanna
To get mod_rewrite and .htaccess working with CakePHP for search engine optimization (SEO), follow these steps:
- Enable mod_rewrite: Make sure that mod_rewrite is enabled in your Apache server. You can check if the module is loaded by running the apachectl -M command. If it's not enabled, you can enable it by running sudo a2enmod rewrite and then restart the Apache server with sudo service apache2 restart.
- Verify .htaccess file: Ensure that CakePHP's default .htaccess file is present in the root directory of your CakePHP installation. This file comes with the framework and is named .htaccess.default. Rename it to just .htaccess.
- Configure Apache virtual host: Add the following lines to your virtual host configuration (usually located in the sites-available directory) to enable the use of .htaccess files:
1
2
3
4
5
|
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
|
- Enable AllowOverride: Open your Apache server configuration file (typically located at /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf) and ensure that the AllowOverride directive is set to All for the relevant directory block. For example:
1
2
3
4
5
|
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
|
- Restart Apache: Save the changes made to the Apache configuration file and restart the Apache server for the changes to take effect. Use the command sudo service apache2 restart or sudo service httpd restart depending on your system.
With these steps, mod_rewrite and .htaccess should now be working with CakePHP, allowing for clean URLs and SEO-friendly URLs.