Earlier I wrote about how to enable Clean URLs on a Drupal installation on a shared hosting service, such as my own provider, InMotion Hosting. Now that I have setup my LAMP Stack on my own Ubuntu machine, I also want to enable Clean URLs for Drupal installations on localhost. To do this, I will work from the command line.
This is Drupal.org's tutorial on the subject, which is helpful for the first steps of the process. At the command line, here are my steps:
- I typed "sudo apachectl -M" - This prints out a list of the enabled modules on my Apache Server. In my case, rewrite_module was not included in the list.
- To enable rewrite_module, I used this simple command: "sudo a2enmod rewrite"
- Then, to check whether my command worked, I again typed "sudo apachectl -M", and rewrite_module was now included in the list of enabled modules running on my Apache server.
- IMPORTANT: Before moving on, I need to restart my Apache server through this command: "sudo service apache2 restart"
So, at this point, I have enabled the rewrite_module on my Apache server. This is where the fun begins.
This article was absolutely essential to me for figuring out how to make everything work. Here are the relevant steps from the article:
- I downloaded the most recent Drupal core, moved the tarball to /var/www/ on my computer (this is the base folder for localhost on my computer), unpacked the tarball, and changed the resulting file to a directory that I want to develop from (e.g., localhost/mydirectory/).
- I typed "cd mydirectory" to get into that directory, and then "ls -a" to list out ALL the files. It is important to see all the files because .htaccess is a hidden file, since it begins with a '.'
- I opened up .htaccess through the command "sudo vi .htaccess" ("sudo" is necessary in order to be able to write to the file, unless you are working as root), and then typed gibberish on a non-commented line (i.e., one that doesn't begin with #).
- I went to /localhost/mydirectory/, and I found that Drupal's installation wizard was greeting me, ready to begin the install. This is actually bad, because it means that the server is completely ignoring the .htaccess file.
- The solution is to direct the Apache Server to check out my .htaccess file through editing the /etc/apache2/httpd.conf file. (See below for full text of code)
- Restart Apache again: "sudo service apache2 restart"
- Go back to localhost/mydirectory to make sure that you are now getting "500 Internal Server Error" rather than the Drupal Installation Wizard. (An error is a good thing here.)
- Then, delete the gibberish out of the .htaccess file, and go ahead and install Drupal through the wizard.
- Once Drupal is installed, navigate to Configuration --> Clean URLs, and then click "Enable."
That's it! Your clean URLs should work now. The only caveat is that you will need to add custom code (modifying the Directory tag only) every time that you want to install a new installation on your LAMP server.
Code for httpd.conf file:
<Directory /var/www/mydirectory/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
The important part is the "AllowOverride All", which may read "AllowOverride None", depending on how everything was installed for you.