I have had some good luck creating user-friendly URLs using Apache mod_rewrite. Hopefully, people will find this information helpful.
At the bottom of this post are some links to resources that give additional details about how and why you might want to do this... to make your URLs easier for users to remember, bookmark, and email to friends.
In this example, I added these rules to my .htaccess file in my web root. If you additionally have access to your server root you might want to add similar statements to the httpd.conf file instead (though you will need to refer to the documentation links to double check for any required syntax modifications).
(If your FTP client does not show hidden files like .htaccess check to see if the FTP client has a preference to "show hidden files" or a preference to modify your LIST command to LIST -al instead.)
Whatever you do, make a backup copy of the files you intend to edit. Adding incorrect entries to these files can result in server configuration errors that will prevent your server from delivering pages. If you are editing your theme.html file to take advantage of these new links, you'll enjoy the peace of mind of backing that up to.
Seriously... back up those files before you start!Each entry should appear on a single line... do not allow the entries to wrap.
You'll notice that each RewriteRule begins with a pattern it is trying to match. This is identified with a beginning ^ and ending $.
At the end of each RewriteRule line in this example, you will see [L]. This tells the server that if it finds the match to stop looking for additional matches.
(There are other things you can refer to the documentation to learn more about).
I added the following lines to the bottom of my .htaccess file and they are working wonderfully:
[size=x-small]
RewriteEngine on
RewriteRule ^signout/$ /user.php?op=logout [L]
RewriteRule ^signout(.*)$ $1 [L]
RewriteRule ^myaccount/$ /user.php [L]
RewriteRule ^myaccount(.*)$ $1 [L]
RewriteRule ^contact(.*)$ /modules/contact$1 [L]
RewriteRule ^products/$ /modules/wfchannel/index.php?pagenum=2 [L]
RewriteRule ^discuss(.*)$ /modules/newbb$1 [L]
RewriteRule ^about/$ /modules/wfchannel/index.php?pagenum=1 [L]
RewriteRule ^about$ /modules/wfchannel/index.php?pagenum=1 [L]
[/size]
Now to explain a few of these entries...
Line 1: RewriteEngine on
This makes the server aware it needs to look for pattern matches.
Line 2: RewriteRule ^signout/$ /user.php?op=logout [L]
When the server gets a request for signout/ it returns the result of /user.php?op=logout
The effect is:
http://www.domain.com/signout/ results as
http://www.domain.com/user.php?op=logoutLine 3: RewriteRule ^signout(.*)$ $1 [L]
When the server matches signout the characters after signout are passed to $1
This is important in this position to make sure that relative paths do not try to operate from the imaginary /signout/ directory.
The effect is:
http://www.domain.com/signout /foofoo results as
http://www.domain.com/foofooI will skip a couple lines since they use the same technique.
Line 6: RewriteRule ^contact(.*)$ /modules/contact$1 [L]
When the server matches contact the characters after contact are passed $1 appended to /modules/contact
The effect is:
http://www.domain.com/contact /foofoo results in
http://www.domain.com/modules/contact/foofooIn these examples, note that when I want the root of a subdirectory to return a single specific item (line 2), I include the explicit command. I place this command above the "catch-all" directory passing (line 3) that occurs when I include (.*) in the rule.
Those are the basics. I hope you have found this helpful. Granted, this is not something everyone wants to take the time to do. But, if you get excited about controlling the URLs your visitors see and use, having the flexibility is wonderful.
Note that I am doing this with XOOPS 2.0.5. To get this to work, currently you need to use Onokazu's perfect one line hack as discussed in this
thread.
Here are links to additional resources:
URLS! URLS! URLS!: Why and how to use mod_rewrite
http://www.alistapart.com/articles/urls/Examples of using mod_rewrite
http://www.engelschall.com/pw/apache/rewriteguide/Apache mod_rewrite manual
http://httpd.apache.org/docs/mod/mod_rewrite.htmlThis article about handling URLs with PHP might also be of interest:
http://www.alistapart.com/articles/succeed/