UPDATE for Drupal 8 / PHP 7.2 / Ubuntu 18.04
1 Enable mod_rewrite as described below
2 Add the following to 000-default.conf as follows:
Use below process to enable clean url with Ubuntu 16.04:
Enable apache mode rewrite –
a2enmod rewrite
put below code into
/etc/apache2/sites-available/000-default.conf
<Directory /var/www/html> Allowoverride All </Directory>
service apache2 restart
The most likely cause is misconfiguration of mod_rewrite or clean URLs on the new server. Confirm this by short-circuiting the clean URL system and requesting pages directly, like this:
http://www.example.com/index.php?q=user
If the login page is shown, then you can assume that Drupal’s page serving mechanism is working properly, but mod_rewrite isn’t working. To determine if mod_rewrite is enabled, create a file called phpinfo.php in the root of your site. This file should be accessible from a web browser and contain this code:
phpinfo();
Ensure that mod_rewrite is enabled
Navigate to phpinfo.php in a browser like this: http://www.example.com/phpinfo.php
On that page, look for a section called “Loaded Modules” and check that mod_rewrite is included. If not, mod_rewrite is not being loaded by apache and will need to be enabled. The process will be different depending on the server platform and apache build. Keep the phpinfo.php file for now – it will be helpful later.
Step 1 — Enabling mod_rewrite
In order for Apache to understand rewrite rules, we first need to activate mod_rewrite
. It’s already installed, but it’s disabled on a default Apache installation. Use the a2enmod
command to enable the module:
- sudo a2enmod rewrite
This will activate the module or alert you that the module is already enabled. To put these changes into effect, restart Apache.
- sudo systemctl restart apache2
mod_rewrite
is now fully enabled. In the next step we will set up an .htaccess
file that we’ll use to define rewrite rules for redirects.
Step 2 — Setting Up .htaccess
An .htaccess
file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess
is critical to your web application’s security. The period that precedes the filename ensures that the file is hidden.
Note: Any rules that you can put in an .htaccess
file can be also put directly into server configuration files. In fact, the official Apache documentation recommends using server configuration files instead of .htaccess
because Apache processes it faster that way.
However, in this simple example, the performance increase will be negligible. Additionally, setting rules in .htaccess
is convenient, especially with multiple websites on the same server. It does not require a server restart for changes to take effect and it does not require root privileges to edit those rules, simplifying maintenance and and making changes possible with unprivileged account. Some popular open-source software, like WordPress and Joomla, often relies on an .htaccess
file for the software to modify and create additional rules on demand.
Before you start using .htaccess
files, you’ll need to set up and secure a few more settings.
By default, Apache prohibits using an .htaccess
file to apply rewrite rules, so first you need to allow changes to the file. Open the default Apache configuration file using nano
or your favorite text editor.
- sudo nano /etc/apache2/sites-available/000-default.conf
Inside that file, you will find a <VirtualHost *:80>
block starting on the first line. Inside of that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.
<VirtualHost *:80> <Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory> . . . </VirtualHost>
Save and close the file. To put these changes into effect, restart Apache.
- sudo systemctl restart apache2
Now, create an .htaccess
file in the web root.
- sudo nano /var/www/html/.htaccess
Add this line at the top of the new file to activate the rewrite engine.
RewriteEngine on
Save the file and exit.
You now have an operational .htaccess
file which you can use to govern your web application’s routing rules. In the next step, we will create sample website files that we’ll use to demonstrate rewrite rules.
Step 3 — Configuring URL Rewrites
Here, we will set up a basic URL rewrite which converts pretty URLs into actual paths to pages. Specifically, we will allow users to access http://your_server_ip/about
, but display a page called about.html
.
Begin by creating a file named about.html
in the web root.
- sudo nano /var/www/html/about.html
Copy the following HTML code into the file, then save and close it.
<html> <head> <title>About Us</title> </head> <body> <h1>About Us</h1> </body> </html>
You can access this page at http://your_server_ip/about.html
, but notice that if you try to access http://your_server_ip/about
, you will see a 404 Not Found error. To access the page using /about
instead, we’ll create a rewrite rule.
All RewriteRules
follow this format:
RewriteRule pattern substitution [flags]
RewriteRule
specifies the directive.pattern
is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser.substitution
is the path to the actual URL, i.e. the path of the file Apache servers.flags
are optional parameters that can modify how the rule works.
Let’s create our URL rewrite rule. Open up the .htaccess
file.
- sudo nano /var/www/html/.htaccess
After the first line, add the RewriteRule
marked in red and save the file.
RewriteEngine on RewriteRule ^about$ about.html [NC]
In this case, ^about$
is the pattern, about.html
is the substitution, and [NC]
is a flag. Our example uses a few characters with special meaning:
^
indicates the start of the URL, afteryour_server_ip/
.$
indicates the end of the URL.about
matches the string “about”.about.html
is the actual file that the user accesses.[NC]
is a flag that makes the rule case insensitive.
You can now access http://your_server_ip/about
in your browser. In fact, with the rule shown above, the following URLs will point to about.html
:
http://your_server_ip/about
, because of the rule definition.http://your_server_ip/About
, because the rule is case insensitive.http://your_server_ip/about.html
, because the original proper filename will always work.
However, the following will not work:
http://your_server_ip/about/
, because the rule explicitly states that there may be nothing afterabout
, since the$
character appears afterabout
.http://your_server_ip/contact
, because it won’t match theabout
string in the rule.
You now have an operational .htaccess
file with a basic rule that you can modify and extend to your needs. In the following sections, we will show two additional examples of commonly used directives.