Mod rewrite Tutorials
overview - syntax - basic - medium - advanced
Mod Rewrite Basic tutorials
The majority of you came to this site to find out how to do one of these
basic tutorials. So lets go ahead and line up what we'll be learning.
Tutorial 2
301 and 302 redirects using mod rewrite
Redirects are some of the easiest rewrites to do. They
are very powerful so becareful. I'll just go over the most popular
and then let anyone that wants to write a note about redirects
post away after the tutorial.
Scenario 1:
We have changed the file name /blue-widgets.html to
/awesome-blue-widgets.html. We need to set up a 301 permanent redirect
so any user or search engine bot that visits the old file will
be redirected to the new.
.htacess code RewriteEngine On
RewriteBase/
RewriteRule ^blue-widgets\.html$ awesome-blue-widgets.html [R=301,L]
In the pattern block we just match the file word for word.
I escaped the . using a \ slash. You really don't have to do this
but I think it's proper coding so I added it in there.
In the rewrite
block you just declare the file you want to redirect
to and finally you need to add the command flag R=301. Las we
add the ,L
to tell the mod that this is our last RewriteRule.
Note: [R] is a default 302. You must declare it as a [R=301] or
you may page hijack your own file. Practical uses for scenario 1
- Redirecting users and search engines from old to the new
file correctly.
- Fixing a mispelled file that has already be cached by the search
engines and redirecting it to the correct spelling of the file.
Scenario
2:
We've changed our domain name. We've set up every
single file on the new domain. All the files will keep the
same name, directory structure and extensions. We need to redirect
users and search engine bots to our new domain via 301 so the
new site starts getting picked up.
.htacces code on the old domain! RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
Easier than you thought it would be. Pretty powerful stuff.
Instead of having to set up a rewrite rule for each file we're
going to capture all the file names and folder names with a wildcard
match (.*). Then in the rewrite block we call it to the end of
our domain name. Final touch is to add the [R=301,L] and you're
done.
Practical uses for scenario 2
- Redirecting users and search engines from old sites to the
new file on a new site correctly.
But wait, there's more...
What if you have query strings on your urls? Easy enough mod
rewrite will call the global server variable for the QUERY_STRING
in the rewrite block. We'll go over that in the medium tutorials
though since I don't want to get into the RewriteCond in the
basics. If you want to skip there now be my guest. skip
to medium mod rewrites >>
Scenario 3:
We have a file called /employment.html on this page
we list all the jobs our company is taking resume's for. At this
time though the page is empty and we want to redirect users to
a temporary page called /no-vacancies.html.
302 means a temporary
redirect. It implies that the page we are redirecting to is
temporarly suppost to take the place of the file that you are
redirected
from.
.htaccess code RewriteEngine On
RewriteBase /
RewriteRule ^employment\.html$ no-vacancies.html [R,L]
Note: Google will use the cache of no-vacancies.html in employment.html
if you check it. It will also enherit any rankings, backlinks,
titles etc that no-vacancies.html has. Be very careful with this.
The last thing you want is to set up a 302 redirect to your index
page and hijack the rankings of that page!
Practical uses for scenario 3
- Redirecting users and search engines to temporary content
files for pages that are being updated or not applicable at
this current time.
<< mod rewrite syntax :: basic
mod rewrites :: medium mod rewrites >> |