HTML5, CSS3, and JavaScript Fourth Edition

(Ben Green) #1

CHAPTER 19. FIRST WEBSITE 215


Two files are involved. The.htaccessfile specifies that passwords are being
used. Another file of your choice, maybe.htpasswd, gives the actual details.


http://www.thesitewizard.com/apache/password-protect-directory.
shtmlhas a very helpful article.


Rewrite


Another popular thing to customize is the rewriting of URLs. For example,
the user might typewww.example.comand you want it to be treated as
though they had typedexample.com, or vice versa. This can be done with
rewrite rules.


Rewrite is particularly handy if you decide to rename a section of your
website. Visitors using the old URL can be directed to the new location.
It also works for renaming a whole website. You might have two domains,
abc.comandalwaysbecorrect.comwhere the short one is the one you want
people to use, and the long one is just another way of saying the name of
your business. You can rewrite the long name as though the user had typed
in the short name.


Rewrite can also control how users view certain directories. Say you have a
directorypublic_html/wpwhere you have installed WordPress. The URL
would beexample.com/wp. But maybe you are using a subdomain, and you
want the URL to bewp.example.com. You can use rewrite rules to change
the incorrect typing into the correct URL.


Here is an example I am using.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^quizgen.doncolton.com$
RewriteRule ^(.*)$ http://quizgen.tk/$1 [R=301,L]


The first line tells Apache to turn on the Rewrite Engine.


The second line says “if” theHTTP_HOSTis exactly quizgen.doncolton.com,
keep going.


The third line says if you get here, change the name to quizgen.tk.


This example is simply to give you a quick idea of what is possible. You can
Google search for “apache rewrite” to get more detailed tutorials.

Free download pdf