Forum Moderators: phranque
Is there anything wrong with this? I know how to use the basic features of mod rewrite but not much else. I know that the order of things factors into mod_rewrite successfully working, but I don't know any particulars.
Any pointers, tutorials, etc much appreciate.
What I use:
ErrorDocument 404 /error/404
ErrorDocument 401 /error/401
ErrorDocument 403 /error/403
ErrorDocument 500 /error/500
RewriteEngine On
RewriteRule ^page/([0-9]*)/?$ /index.php?newspage=$1 [L]
RewriteRule ^about/?$ /index.php?contentpage=coabout [L]
RewriteRule ^morelinks/?$ /index.php?contentpage=colinks [L]
RewriteRule ^posts/?$ /index.php?contentpage=coposts [L]
RewriteRule ^galleries/?$ /index.php?contentpage=coposts&specifics=galleries [L]
RewriteRule ^posts/([0-9]*)/?$ /index.php?contentpage=coposts&story=$1 [L]
RewriteRule ^error/([0-9]+)/?$ /index.php?contentpage=coerror&error=$1 [L]
RewriteRule ^sitedocs/?$ /index.php?contentpage=cositepages&sitepage=index [L]
RewriteRule ^sitedocs/([a-zA-z]+)/?$ /index.php?contentpage=cositepages&sitepage=$1 [L]
Mod_rewrite can't be used by everyone; your server must have it loaded, enabled, and have other higher-level settings to allow it to run. Some of these settings may be under your control, and some not -- it depends on your privilege level and how your hosting account is set up.Here's a list of directives and settings you'll need to enable mod_rewrite:
LoadModule rewrite_module <your_modules_path>/mod_rewrite.so AddModule mod_rewrite.c AllowOverride FileInfo Options -or- AllowOverride All Options +FollowSymLinks -or- Options +SymLinksIfOwnerMatch -or- Options All Addmodule, LoadModule, and AllowOverride are available at the server configuration level only, usually in the httpd.conf file. If you are on a shared server these will not be accessible. You can try adding the Options directive to your .htaccess file and testing with a simple rewrite, but if this doesn't work, you'll need to ask you host for help with LoadModule, AddModule and AllowOverride settings. If they won't help, consider changing hosts.
One problem that crops up often is that of LoadModule order, particularly it seems on home-brew PHP installations. It's important to realize that Apache modules are executed in reverse order from that specified in the list of LoadModule directives. If you load PHP after loading mod_rewrite and set up the server to parse certain file types for PHP, then mod_rewrite won't be executed for any of those files that exist; PHP will be invoked first and it will bypass mod_rewrite. The php module should be loaded before mod_rewrite.so to avoid this problem.
Jim