Change background image

TUTORIALS Setup mod_rewrite for Apache on Ubuntu 14.04

Discussion in 'Website & Server Administration' started by DannyTuan, Jun 14, 2018.

  1. DannyTuan

    DannyTuan Ban Quản Trị Staff Member Administrator

    Jun 14, 2018
    Danny Production
    Home Page:
    With this tutorial, you need:
    • One VPS with Ubuntu 14.04.
    • A sudo non-root user, which you can set up by following steps 2 and 3 of this tutorial.
    Step 1 — Installing Apache
    In this step, we will use a built-in package installer called apt-get. It simplifies management drastically and facilitates a clean installation.
    First, update the system's package index. This will ensure that old or outdated packages do not interfere with the installation.
    Apache2 is the aforementioned HTTP server and the world's most commonly used. To install it, run the following:
    For information on the differences between Nginx and Apache2, the two most popular open-source web servers, see this article.
    Step 2 — Enabling mod_rewrite
    Now, we need to activate mod_rewrite.
    This will activate the module or alert you that the module is already in effect. To put these changes into effect, restart Apache.
    Step 3 — Setting Up .htaccess
    In this section, we will setup a .htaccess file for simpler rewrite rule management.
    A .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.
    We will need to set up and secure a few more settings before we can begin.
    First, allow changes in the .htaccess file. Open the default Apache configuration file using nano or your favorite text editor.
    Inside that file, you will find the <VirtualHost *:80> block on line 1. Inside of that block, add the following block:
    <Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    Your file should now match the following. Make sure that all blocks are properly indented.
    <VirtualHost *:80>
    <Directory /var/www/html>
    . . .
    . . .
    To put these changes into effect, restart Apache.
    Now, create the .htaccess file.
    Add this first line at the top of the new file to activate the RewriteEngine.
    RewriteEngine on
    Save and exit the file.

    To ensure that other users may only read your .htaccess, run the following command to update permissions.
    You now have an operational .htaccess file, to govern your web application's routing rules.
    Step 4 — Setting Up Files
    In this section, we will set up a basic URL rewrite, which converts pretty URLs into actual paths to code. Specifically, we will allow users to access

    We will begin by creating a file named about.html.
    Copy the following code into the HTML page.
    <title>About Us</title>
    <h1>About Us</h1>
    You may access your web application at your_server_ip/about.html or Now notice that only about.html is accessible; if you try to access your_server_ip/about, you will get a Not Found error. We would like users to access about instead. Our rewrite rules will allow this very functionality.

    Open up the .htaccess file.
    After the first line, add the following.
    RewriteRule ^about$ about.html [NC]
    Your file should now be identical to the following.
    RewriteEngine on
    RewriteRule ^about$ about.html [NC]
    Congratulations. You can now access in your browser!

    This is a good simple example that shows the general syntax that all Rewrite Rules follow.

    ^about$ is the string that gets matched from the URL. That is, it's what the viewer types in her browser. Our example uses a few metacharacters.
    • ^ indicates the start of the URL, after is stripped away.
    • $ indicates the end of the URL
    • about matches the string "about"
    about.html is the actual path that the user accesses; that is, Apache will still serve the about.html file.

    [NC] is a flag that ignores capitalization in the URL.

    With the rule shown above, the following URLs will point to about.html:
    The following will not:

Share This Page