Think about the last time you visited some shopping website, looking for that one specific thing you needed to buy. When you finally reached the page, the URL most likely looked something like this:
gizmo.com/latest_and_greatest/specific_gadgets/exactly_what_youre_looking_for
This is not because this website took the time to set up every single directory you would need to make your purchase, but because of a handy module called Mod_Rewrite. Mod_Rewrite allows you to make custom and simplified URLs as needed. In reality, the actual URL may have looked closer to this:
http://www.gizmo.com/gp/itemB004RYVI0Q/ref=as_li_ss_tl?
This tutorial will go over Activating Mod_Rewrite, Creating and Using the required .htaccess page, and setting up the URL rewrites.
The steps in this tutorial require the user to have root privileges. You can see how to set that up on Ubuntu here, in steps 3 and 4.
Additionally, you need to have apache installed on your server. If you do not have it, you can download it for Ubuntu with this command:
sudo apt-get install apache2
Before we begin generating the actual URL rewrites, we need to activate the apache mod_rewrite module that controls them. This is simple:
sudo a2enmod rewrite
The command activates the module or—if it is already activated, displays the words, "Module rewrite already enabled"
Once the module has been activated, you can set up your URL rewrites by creating an .htaccess file in your website directory.
An .htaccess file is a way to configure the details of your website without needed to alter the server config files. The period that starts the file name will keep the file hidden within the folder.
Additionally the placement of the .htaccess file is important. The configurations in that file will affect everything in its directory and the directories under it.
You can create the .htaccess file in a text editor (make sure to name it only .htaccess without any other extension or name) and then upload it to your site through an ftp client.
Alternatively you can use this command, replacing the example.com with the name of your site, to create your .htaccess file in terminal.
sudo nano /var/www/example.com/.htaccess
To allow the .htaccess file to override standard website configs, start by opening up the configuration file. NB: You will need sudo privileges for this step.
sudo nano /etc/apache2/sites-available/default
Once inside that file, find the following section, and change the line that says AllowOverride from None to All. The section should now look like this:
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
After you save and exit that file, restart apache. .htacess files will now be available for all of your sites.
sudo service apache2 restart
Now you are all set up to rewrite your site’s URLs.
The entire URL rewriting operation takes place within the .htaccess file.
Overall, all of the URL rewrite commands follow the same pattern:
RewriteRule Pattern Substitution [OptionalFlags]
Here is a short explanation of each part:
Example 1: Go to Page A, find page B:
This is the most basic example for a URL rewrite: a visitor to the site types one URL into the browser but is redirected to another. Here is how to set it up.
Lets go ahead and make two separate pages on for a site—say, one for Apples (apples.html) and one for Oranges (oranges.html):
Copy the code into the Apple page:
<html> <head> <title>Apples</title> </head> <body> <h1>This page is about Apples</h1> </body> </html>
After that, can make the orange page, substituting all the fruit names to refer to the appropriate one.
Now open up the .htaccess file.
sudo nano /var/www/example.com/.htaccess
Add the following URL rewrite commands to the file:
RewriteEngine on RewriteRule ^oranges.html$ apples.html
Save and exit.
Once everything is in place, visit the site ending in "/oranges.html"— all of the information displayed will come from the "/apple.html" site.
Now for an explanation:
Example 2: The website has a parameter in its URL. How to make it look like a subdirectory.
The first example referred to a site that simply needed to be substituted with another one. The instance below, however, addresses a common scenario that can be seen when there is a parameter in the url.
Check out this URL:
http://example.com/results.php?products=apple
It would be much clearer displayed as:
http://example.com/products/apple
The lines within the .htaccess file would look like this:
RewriteEngine on RewriteRule ^products/([A-Za-z0-9-]+)/?$ results.php?products=$1 [NC]
Now for an explanation:
Example 3: The site has an unwieldy URL. How to clean it up
This sort of situation can arise when URLs are long and complex.
Take the URL below as an example:
http://example.com/results.php?products=produce&type=fruit&species=apple
As effective as the URL is in delivering the correct content, it is not very memorable for the consumer. URL rewrites would allow you to convert the URL to something simpler and clearer:
http://example.com/produce/fruit/apple
In order to accomplish this, we would need the following lines in our .htaccess file (you can add as many section as needed in the .htaccess file):
RewriteEngine on RewriteRule ^(meat|produce|dairy)/([^/.]+)/([^/.]+)$ results.php?products=$1&type=$2&species=$3
Now for an explanation:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Please note that when you use the LAMP stack application to install your droplet that /etc/apache2/sites-available/default is actually located in the file /etc/apache2/sites-available/000-default.conf.
The file should have this appended:
<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
For apache version 2.4 and later, you have to go to: **/etc/apache2/apache2.conf **
You have to edit that file with sudo permission. Change like this:
then restart apache:
service apache2 restart
Hope it helps
Shouldn’t
sudo nano /etc/apache2/sites-available/default
besudo nano /etc/apache2/apache2.conf
?nice explanation thanks
I will marry whoever wrote this article, the pain I have been through and you solved it with a word!
Hi, could you please help me how can i make my url SEO friendly. I tried rewrite using htaccess but not at all working. I didnt quite understand this section in your example.
“^products: In order to be caught and rerouted, the URL must start with products (keep in mind that this only refers to the text after the domain name). Should it begin with anything else, the rule will not apply and the URL will stay the same.”
My URL would look like, http://www.tasteofkochi.com/dine-detail.php?a=280
I want it to be something like http://www.tasteofkochi.com/dine-detail/280
Below is my htaccess
RewriteEngine On RewriteRule ^dine-detail/(0-9]+)/?$ dine-detail.php?a=$1 [NC]
Could you please advise, how to modify this. I believe I am doing something terribly wrong here.
I have done all setting correct in httpd.conf - like rewrite on and <directory> setting
Thanks Bino
You should be very cautions about allowing all overrides in your .htaccess file. It’s far better to only allow what you need. Instead of using…
… try …
… instead
In the example titled Example 2: The website has a parameter in its URL. How to make it look like a subdirectory, if I do what is shown then it shows data from http://example.com/results.php?products=apple when url http://example.com/products/apple is entered in the url bar but it doesn’t change page to http://example.com/products/apple when http://example.com/results.php?products=apple is in the url bar.
Any thoughts…
I wonder if anyone can help… we have a folder /player/ which is where all our articles reside, but wish to change the folder to /article/123aB/title-goes-here.
I’ve been battling with .htaccess for a few weeks, with no luck so far as getting a 404 error
RewriteBase /player/ RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?id=$1&article=$2 [L]
But also need this to work, if someone comes in on… http://www.domian.com/player/?id=123aB
Can you point me in the right direction?
Perfect !! It’s working. Article is nicely written.
Solved my problem after applying both.