Beruflich Dokumente
Kultur Dokumente
You are here: Home » Howtos » Web Server » Apache » The Useful Uses Of Mod Rewrite
Navigation
The Useful Uses Of Mod Rewrite
Howtos
Linux
Want to support HowtoForge? Become a subscriber!
Android
CentOS Submitted by MVied (Contact Author) (Forums) on Thu, 2008-1 1-27 12:28. :: Apache
Debian
Tweet
Fedora
Kernel
The Useful Uses Of Mod Rewrite
Mandriva
Author: Mike Ems
PCLinuxOS
In this tutorial, I'm going to be teaching what mod rewrite is and few
SuSE
examples of its uses. Mod rewrite is a powerful tool and one of the
Ubuntu
simplest ways to make your website more SEO friendly.
Web Server
What is Mod Rewrite?
Apache
Mod rewrite is a part of apache servers that can rewrite requested URLs
Cherokee on the fly. As it supports an endless number of rules that in turn have
Lighttpd unlimited attached rule conditions it is very flexible and an important URL
manipulation mechanism. It can be used for internet users and for search engine friendly URLs. This increases the chance
nginx
of the database driven website to be indexed.
Backup
Enabling / Installing Mod Rewrite
Control Panels
The first thing we want to do is make sure that we have mod_rewrite on our server. By default, Apache has mod_rewrite
ISPConfig
installed, but not enabled. The easiest way to test if mod_rewrite is enabled on your server is to create a .htaccess file in
DNS a test directory such as /modrewrite-test/.htaccess with the following inside:
Anti-Spam/Virus 1. Find the httpd.conf file (usually you will find it in a folder called conf, config or something along those lines)
Postfix 2. Inside the httpd.conf file uncomment the line LoadModule rewrite_module modules/mod_rewrite.so (remove the pound '#'
sign from in front of the line)
FTP
3. Also find the line ClearModuleList is uncommented then find and make sure that the line AddModule mod_rewrite.c is not
High-Availability
commented out.
Monitoring After these steps, restart your apache server with the following command.
MySQL
/etc/init.d/httpd restart
Programming
You should now have mod_rewrite successfully enabled. Be sure to test mod_rewrite again once apache server has been
C/C++
restarted.
PHP
Using Mod Rewrite To Always Remove / Add WWW
Samba
One easily fixed problem that many websites have is duplicate results in search engines. In any given search engine,
Security
http://www.example.com/ and http://example.com are two completely different websites. This is a big problem because
Anti-Spam/Virus the content in your website is identified as duplicate content, and that brings your search engine rankings down. Using
our new friend, mod_rewrite, we can assess this issue.
Storage
Virtualization At the top of any .htaccess file, you need to turn Mod Rewrite on. This should always be at the top of your .htaccess file.
You only need to turn Mod Rewrite on once.
KVM
OpenVZ RewriteEngine On
VMware Additionally, you need to set your Rewrite Base. This is normally the directory in which the .htaccess file is located. If
VirtualBox your .htaccess file is located in your root folder, your Rewrite Base would look like this:
Xen RewriteBase /
Other Now then, there are two options to choose from. You can either choose to always remove the www, or always keep it. To
http://www.howtoforge.com/the-useful-uses-of-mod_rewrite Page 1 / 4
FreeBSD always remove www (my preferred method), create a .htaccess file in your root directory. Inside of that file, add the
following:
Commercial
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
Mini-Howtos
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Linux
Likewise, if you would like to always add www, add the following to your .htaccess file:
Apache
Backup RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]
DNS
Errors Be sure to change example.com to your domain.
FTP
Using Mod Rewrite to Clean Up Your URL's
MySQL The most popular use of Mod Rewrite is using it to clean up your website's URL's. As you may or may not know, search
Networking engines don't particularly care for URL's with a lot of arguments like many websites serve. Example:
PHP http://www.example.com/index.php?p=about
Postfix
With the power of the mod_rewrite apache module, we can make this URL much cleaner.
Security
http://www.example.com/about.html
Sendmail
Shell To accomplish this, we get to see our good friend regular expressions. I know you're really excited now. To accomplish the
rewrite above, I added the following to my .htaccess file after the add/remove www lines.
Other
ISPConfig RewriteRule ^([^/]+).html$ /index.php?p=$1 [QSA,L,NC]
Forums Let's take a closer look at this line so that we can really understand what it's doing.
Contribute ([^/]+)
Create Content This is the regular expression to match anything except a forward slash. This is the easiest way to match anything that
will fall between two forward slashes when dealing with rewriting URL's.
Subscription
Login .html
Site Map/RSS This rewrites the url to have .html at the end. This gives the appearance of a regular .html file to your php file.
Feeds
/index.php?p=$1
This is the part of the rewrite which is the originating location. Each time you have a regular expression in the first part
User login matching a certain string, you should have it referenced in this part. The $1 references the first regular expression in the
Username: first half of the line. So any value that is assigned to the variable p is placed where the first regular expression is in the
first half of the line.
Password:
If you wanted your URL to look like a directory, you would use this line instead of the one given previously.
cRemember Me?
d
e
f
g RewriteRule ^([^/]+)/?$ /index.php?p=$1 [QSA,L,NC]
rewriteCond $1 !/$
rewriteCond %{REQUEST_FILENAME}/ -d
HowtoForge
rewriteRule (.+) http://www.example.com/$1/ [R=301,L]
Like
The first line checks to see if there is no trailing slash. The second line checks to see if a directory exists with the given URL
Facebook social plugin
if the trailing slash is added. The third line adds the trailing slash and redirects the user.
http://www.howtoforge.com/the-useful-uses-of-mod_rewrite Page 2 / 4
Cant get SMTP AUTH
to work
Ispconfig3/postfix Re: How Does A Trailing Slash Decrease Server Traffic?
One database user Submitted by David C. Rankin... (not registered) on Wed, 2009-02-04 18:37.
more db
Basically,
Root mail messages
eth0 now eth2 in The web addresses of:
debian http://myserver/mydir -- and --
Block Access to
http://myserver/mydir/
isp config
help me plz ?? are two separate 'contexts' for the web server which require the server to analize the security access requirements of mydir
twice if the trailing '/' is omitted.
The way I understand it is, when you request //myserver/mydir, the server checks the security context of pages in your
News 'document root' (or whatever the parent directory is) to see if you have access to a page called 'mydir'. When it discovers that
'mydir' is a directory, the server appends the trailing '/' and then it has to repeat the entire process again for the directory 'mydir/'
How to setup and expand before providing access to the index.html or other page in the 'mydir' directory.
a replicated Glusterfs 3.2.4
volume on Debian 6.x Conversely, if you had requested mydir/ originally, there is one the single security context check for the directory mydir before
Valve Gets Into Wearable access to the pages are provided.
Computing, Blogging Simply put if you just put mydir, then the server checks the parent directory for access permissions, finds that mydir is really
Science the GNU Way, Part the directory mydir/, appends the /, and then has to do the checks all over again for mydir/.
I
So do your server a favor and always append the trailing / to directory names ;-)
Fedora 17 Sneak Peek
KDE4 Activities for Fast reply | view as pdf
Efficient Workflow
Fedora 17 Beta is now
available
Implement strong WiFi
encryption the easy way
with hostapd
Reading Between the
Linux Contributor List's
Lines
Rackspace Declares War
Against Proprietary Clouds
Danny Pop: Give me my
Linux and computers back!
more
Recent comments
Temp space
12 hours 10 min ago
Re: htpasswd2 for Debian
Lenny does not exist use
this command
13 hours 31 min ago
Packages
14 hours 21 min ago
Packages
16 hours 34 min ago
Re: Typo? DATABASE
ERROR: could not lookup
old password:
16 hours 49 min ago
help me ?plz
18 hours 21 min ago
thanks
19 hours 36 min ago
You should not trust in
1 day 1 hour ago
Re: Re: Re: Steps 9 and
10
1 day 7 hours ago
A few problems
1 day 9 hours ago
Newsletter
Subscribe to
HowtoForge
Newsletter
and stay informed about
our latest HOWTOs and
projects.
enter email address
Submit
http://www.howtoforge.com/the-useful-uses-of-mod_rewrite Page 3 / 4
(To unsubscribe from
our newsletter, visit this
link.)
http://www.howtoforge.com/the-useful-uses-of-mod_rewrite Page 4 / 4