Forum Moderators: phranque
I'm new to using the mod_rewrite module, I just tried for the first time, and what do you know - I can't get it to work. I have Apache running on my PC. I've enabled mod_rewrite in httdpd.conf by uncommenting:
LoadModule rewrite_module modules/mod_rewrite.so
(and it seems to be loaded when checked via phpinfo(); )
I have an .htaccess file in my 'server' folder, very simple:
Options +FollowSymLinks
#Options +Indexes
RewriteEngine on
RewriteRule ^/([a-z]+)/?$ file.php?cat=$1 [NC,L]
but if I try [localhost...] I get a 404 Not Found :(
I enabled debugging, but it just created an empty file and won't log anything.. Any advice?
On a secound note, I remember reading somewhere about using a file with a list of valid URL's and having apache check these. For example, say I have some preset categories, so www.mysite.com/cat1 would be rewritten to /page.php?cat=cat1 IF cat1 is listed as a valid category, otherwise www.mysite.com/sometxt would be rewritten to something like /notfound/.
If I list the valid categories in a file, is there a way to have apache check the requested url with this list?
I hope I'm making sense. Thanks.
If your code is in a .htaccess file, remove the directory path to the .htaccess file from all RewriteRule patterns in that .htaccess file. In other words, it appears that you need to remove the leading slash from your RewriteRule pattern.
See if that helps, and then we'll get on with part two... :)
Jim
Anyway, as for my secound question, I'm sure I've read about using files (or maps?), but can't find where.
Positive matches:
RewriteCond %{REQUEST_URI ^other-valid-url-1$ [OR]
RewriteCond $1 ^valid-url-1$ [OR]
RewriteCond $1 ^valid-url-1$
RewriteRule (.*) /new-path [L]
Negative matches:
RewriteCond $1 !^exception-url-1$
RewriteCond $1 !^exception-url-1$
RewriteCond %{REQUEST_URI !^other-exception-url-1$
RewriteRule (.*) /default-path [L]
The next step up is to use a RewriteMap. You can use a plain-text map file or a NDBM-format file to do lookups, and note that the rule itself can specify a default URL for the case where the map does not return a looked-up value.
Finally, RewriteMap also has an option to call a script. So you might write a small PERL script to open your CMS or shopping cart database and get the URL-translation from that database. This offers maximum flexibility, and completely-centralized administration -- You can potentially manage the URL-translations using the same tools you use to manage your CMS or shopping cart. Using a very-specifically-tagged URL is pretty important in this case; You want the RewriteRule to be able to identify only those URLs which need to be translated, in order to avoid calling your database when it's not necessary.
Jim
I have another 'issue'. I'm trying to block all direct access to my php (or other) files.
RewriteCond %{THE_REQUEST} ^(GET¦HEAD¦POST)\ /.+\.PHP [NC]
RewriteCond %{IS_SUBREQ} ="false"
RewriteRule \.php$ - [NC,L,F]
However, since .htaccess works only on HTTP requests, this problem indicates that your includes are coded to request included object using HTTP, instead of doing direct filesystem reads. Changing those includes to direct filesystem accesses could very well triple the speed of your site...
Jim
well, thanks for the fast response :)
There we go, found this:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.
Thanks again!