Forum Moderators: phranque

Message Too Old, No Replies

Trouble with my htaccess file

         

formasfunction

8:30 am on Apr 12, 2008 (gmt 0)

10+ Year Member



I'm not sure what the problem is but these rewrites aren't working:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/$ /index.php?module=welcome [L,QSA]
#
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)$ /index.php?module=$1 [L,QSA]
#
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)$ /index.php?module=$1&class=$2 [L,QSA]
#
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)/([^/]*).html$ \
/index.php?module=$1&class=$2&event=$3 [L,QSA]

Any ideas about what I'm doing wrong here? It's giving me 404s.

jdMorgan

1:28 pm on Apr 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule patterns in .htaccess should not start with "/".

.htaccess is a "per-directory" configuration file. Therefore the path to the current directory --in this case "/"-- is removed from the URL-path "seen" by RewriteRule.

It should not be necessary to include "%{DOCUMENT_ROOT}/" when using %{REQUEST_FILENAME}

The "([^/]*)" subpatterns are open to a common error: the occurrence of multiple consecutive slashes. When these occur (mostly due to typing errors), Apache ignores them, but your rules will behave differently. To prevent this problem, I'd suggest using "([^/]+)" instead.

The processing of the final subpattern in the last rule can be sped up by telling the regex parser to quit processing that subpattern if it finds a period in a request that will subsequently match, as well as quitting when it find a slash in a request which will not subsequently match (as you've already done).

Your last rule also contains an unescaped literal period.

So, rolling this all together and using your last rule as an example, I'd suggest:


RewriteCond [b]%{REQUEST_FILENAME}[/b] !-d
RewriteCond [b]%{REQUEST_FILENAME}[/b] !-f
RewriteRule [b]^([^[/b]/][b]+)[/b]/([^/][b]+)/([^/.]+)\.h[/b]tml$ /index.php?module=$1&class=$2&event=$3 [L,QSA]

Be aware that the file-exists and directory-exists checks are quite expensive in terms of server performance; Each requires a call to the operating system to go check the file system, and may invoke a disk read to get the current directory and file index from the disk for checking. Therefore, the patterns for rules which use "-f" and "-d" should be as specific as possible in order to avoid unnecessary disk checks. By way of explanation, see the mod_rewrite documentation, which states (in more detailed form) "RewriteConds are not processed if the RewriteRule pattern does not match."

Your first rule is of a form not usually needed. This function is typically implemented using the DirectoryIndex directive:


DirectoryIndex /index.php?module=welcome

Jim

formasfunction

3:35 pm on Apr 12, 2008 (gmt 0)

10+ Year Member



Wow Jim, wonderful response. I appreciate not only the help but the clear explanations; they'll help me avoid the same mistakes in the future and it's working perfectly now. Thanks!