Sie sind auf Seite 1von 4

       

Register Login Contribute Subscribe RSS

Howtos Forums Contribute Subscribe ISPConfig FAQForge News

English | Deutsch | Site Map/RSS Feeds | Advertise


Options For This
Howto
Free Support
Paid Support

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:

BIND Options +FollowSymLinks


MyDNS RewriteEngine On
PowerDNS Now attempt to browse to the subdirectory. One of two things could happen:
djbdns
• No errors Congrats mod_rewrite engine is now enabled.
Desktop • 500, Internal Server Error If you get this message then mod_rewrite was not installed/enabled on your server.
Email If you found that mod_rewrite was not installed on your server, follow these steps.

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]

Log in This would return URL's like this:

Create a new account http://www.example.com/about/


Request new password
Adding a Trailing Slash To Your URL's
Facebook The main point of adding a trailing slash to your URL's is that it increases server performance. When a trailing slash is
added to the end of a URL that does not end in a file extension, it tells the server to only make on call to itself, thus
decreasing server traffic.
Find us on
Facebook To always add a trailing slash to a URL that does not end in a file extention, such as http://www.example.com, add the
following to your .htaccess file in your root directory.

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.

Who's online Copyright © 2008 MVied


There are currently 9 users All Rights Reserved.
and 3791 guests online.
Tweet

HowtoForge add comment | view as pdf | print


Forums
DNS not working
Please do not use the comment function to ask for help! If you need help, please use our forum.
with ISPconfig3
Comments will be published after administrator approval.
how to track or
trace outgoing
traffic
How Does A Trailing Slash Decrease Server Traffic?
RAM problem
Submitted by AceFrahm (registered user) on Tue, 2009-01-27 22:56.
IPVS: WLC: no
destination What happens when there is no trailing slash?
available
reply | view as pdf

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.)

Howtos | Mini-Howtos | Forums | News | Search | Contribute | Subscription


Site Map/RSS Feeds | Advertise | Contact | Disclaimer | Imprint

Copyright © 2012 HowtoForge - Linux Howtos and Tutorials


All Rights Reserved.

http://www.howtoforge.com/the-useful-uses-of-mod_rewrite Page 4 / 4

Das könnte Ihnen auch gefallen