Forum Moderators: phranque

Message Too Old, No Replies

Some mod rewrite issues

         

ag_47

12:30 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Hi,

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.

jdMorgan

1:26 am on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's have one question at a time, please... No use confusing the thread at the start...

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

ag_47

2:41 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Already tried that, doesn't help. Even when I try a direct method like:
RewriteRule ^tst.php$ page.php [NC,L,R] //I see tst.php.

I also don't like, and don't see why the error log file was created but stays empty.. I used
RewriteLog C:\SERVER\log.txt
RewriteLogLevel 9 in httpd.conf

Thanks.

ag_47

3:21 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Got it! You were right, the leading slashed were unnecessary. But the reason it didn't work was because of Options +FollowSymLinks, I needed to set AllowOverride in httpd.
I also noticed that each and every request, including images, css, js files within the pages was tested against these rules.. wow.

Anyway, as for my secound question, I'm sure I've read about using files (or maps?), but can't find where.

g1smd

1:17 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** I also noticed that each and every request, including images, css, js files within the pages was tested against these rules.. wow. ***

Yes. Even robots.txt will be tested.

That's why you need to make sure your rule only covers what it actually needs to cover.

jdMorgan

5:36 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For short, static lists of URLs, you can use the RewriteCond directive to test either a back-reference to the part of the requested URL captured by your RewriteRule, or the server variable %{REQUEST_URI}.

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]

This doesn't work well if you've got more than a dozen URLs or so to test, though, or if the URLs change frequently -- basically it's only moderately efficient and doesn't lend itself to easy maintenance.

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

ag_47

9:46 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



Thanks a lot for the help.

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]

This seems to works, BUT my includes in php scripts also get blocked... Isn't {IS_SUBREQ} supposed to prevent exactly that?

jdMorgan

9:50 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not necessarily.

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

ag_47

10:08 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



I see.
The reason I enabled URL includes is because I need to pass parameters to one particular included file - a global header/navmenu for all pages, which displays a little variation of the navmenu depending on the location passed. The only way I see around this is maybe add an exception for this file in the rewriteCond.

well, thanks for the fast response :)

jdMorgan

10:20 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Passing parameters should still be possible -- It works with URL-to-filename rewrites, and with SSI and PERL file includes using query strings, and I would think it should also work with PHP.

Jim

ag_47

10:44 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



No, says can't open stream, because it's trying to open the whole include parameter as a single file. (like 'file.php?param=123').

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.

works..

Thanks again!