Easy Clean URLs with Apache’s mod_rewrite
First of all I would like to get the following out of my system:
Tutorials are supposed to be EASY! How on earth do you expect someone who is just learning to understand regular expressions, you jerk!
That felt good. Now lets move on, shall we?
You’ve seen it being used everywhere. What is a “Clean URL” and why is it so important today?
- It’s better for Search Engine Optimization (SEO) because it fools the search engine into thinking that the page is actually a folder or a file (depending on how you’ve written it), rather than a long URL with a thousand variables which even a regular person would think twice about crawling.
- It demonstrates knowledge on part of the developer. Since this is slowly becoming a standard of sorts, it’s a must-know for developers.
- It looks nice and clean
Okay, now what do you need to make this work?
- An Apache server with mod_rewrite function on. The following technique works ONLY on an Apache server with mod_rewite enabled. That’s right. If you use a Windows server:
- You’d better be using some Microsoft Technology like ASP, ASP.NET, etc. or you’re wasting your money!
- Here is an article to get clean URLs on the Windows (IIS) server.
- The ‘.htaccess’ file
That’s it!
Now, lets start. Assuming you have a web site called http://www.mywebsite.com and you have a page URL that looks like: http://www.mywebsite.com/index.php?page=green
UGLY!
Here is how you achieve clean URLs:
1. Open the .htaccess file on your root folder. If you don’t already have one, make one in Notepad or whatever, but make sure you name it ‘.htaccess’ (so that’s no name with the file extension ‘htaccess’) and upload it to your root folder (this is usually www or public_html)
2. If your .htaccess file already existed and you have some code in it already, do not overwrite it.
3. Take a backup of the .htaccess file (in case something goes wrong) . Paste the following code:
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-z\-]+)$ index.php?page=$1 [L]
</IfModule>
Note: If the line RewriteEngine on already exists in your .htaccess file, don’t paste that again.
Voila! You now have a clean URL. Now if you navigate to http://www.mywebsite.com/green , you get your page!
How does this work? The text following “RewriteRule” is called a regular expression. It tells Apache to match a condition and if a match is found, redirect (rewrite) internally to a page.
It’s really simple:
^([a-z\-]+)$ : This part is the matching part. This means that lowercase letters a through z and the hyphen (-) are accepted. In our case, “green” falls under this category, so its counted as a “match”
index.php?page=$1 [L] : This is the part that is executed if a match is found. It tells Apache that whatever you found before the “$” is now $1. If you have two conditions, the first one would be $1 and the second $2 and so on. In our case we just matched one, so that’s stored in $1. So now $1 = green. And the page is directed to index.php?page=green .
Sweet! But what’s that [L]? It tells Apache that this is the last statement it needs to execute if a match is found and to stop right there. Just to save on time.
So basically, this entire thing is made up of:
- A condition which checks for a set. (Cause)
- A result statement which does something when a match is found. (Effect)
For more in-depth reading on this subject, click here. For another simple tutorial, click here.
Problems:
“My page loads with mod_rewrite but my style sheets and/or images don’t work!”
Obviously! Now that your browser believes that http://www.mywebsite.com/green is an actual folder, it tries to link it up from there.
Quite simply, if your code on your page for your style sheet is: <link href=”assets/css/style.css” /> it looks for it under http://www.mywebsite.com/green/assets/css/style.css , which doesn’t exist.
Simple solution:
Right after your <html> tag, insert this: <base href = “http://www.mywebsite.com/” />
Now you’re telling the browser, “Hey, from now on all links to resources on my page are to start from my base URL, okay?”
So now the browser loads http://www.mywebsite.com/(the base URL)assets/css/style.css
Problem solved.
“This just doesn’t work!”
Make sure you are running an Apache server with mod_rewrite on. If you’re not sure, contact your web hosting company.
Andrew D 6:07 pm on October 11, 2010 Permalink |
What if I am not using php? I just want to rewrite http://mywebsite.com/vegas to http://mywebsite.com/vegas.html …. What is the code for that?
Andrew D 6:30 pm on October 11, 2010 Permalink |
Hey…I figured it out about the rewrite so thanks! I wanted to ask one more thing. Do you know how to code so I can stealth the first URL. What I mean is , I want the visitor to type in a URL and for that URL to stills show in the address bar (just for appearances) even after we are redirected to the destination URL?
junaidbhura 9:32 am on October 12, 2010 Permalink |
You’ll have to use IFRAMEs for that, which is not recommended because that would be bad for SEO.
To achieve all these ‘cool’ things you’ll need to use a server-side language like PHP which can decide what content to display based on the URL and any other conditions you might have.
Renato 8:19 pm on November 28, 2010 Permalink |
Mmh… What if I am in a subfolder? For istance:
http://www.mysite.com/folder/green -> http://www.mysite.com/folder/index.php?page=green
I tried with RewriteBase, without any success…
junaidbhura 9:35 am on November 29, 2010 Permalink |
You’ll need to put .htaccess in that subfolder. Can I see your htaccess code?
Renato 4:09 pm on November 29, 2010 Permalink |
I tried this: (/folder/.htaccess)
RewriteEngine On
RewriteRule ^([a-z\-]+)$ index.php?page=$1 [L]
And this:
RewriteEngine On
RewriteRule ^([^/\.]+)$ index.php?page=$1 [L]
junaidbhura 4:17 pm on November 29, 2010 Permalink |
Okay.. assuming that your address is:
http://localhost/folder1/folder2/index.php?page=green
and your .htaccess file is in folder2, the first one should have worked: http://localhost/folder1/folder2/green
If it doesn’t, check if mod_rewrite is enabled.
Renato 11:24 pm on November 29, 2010 Permalink |
Tried exatly the first situation, without any success… I’m pretty sure mod_rewrite is enabled, there is a working wordpress on the same server with friendly urls!
Renato 12:08 am on November 30, 2010 Permalink |
Sorry for the mess, the problem was a “AllowOverride None” in the default site’s configuration file.
To avoid the stylesheet and images problem, i used:
RewriteEngine on
RewriteRule ^(.*).html$ index.php?&page=$1 [L]
I think that is good for SEO too, right?
junaidbhura 10:10 am on November 30, 2010 Permalink |
Ah
. Yeah that’s good, it ‘fools’ the search engines into thinking they’re HTML pages.
sumit 3:42 pm on March 15, 2011 Permalink |
no man its not working any other idea to let me out of this
junaidbhura 3:57 pm on March 15, 2011 Permalink |
Can you paste your code here so I know what you’ve done?