.htaccess mod_rewrite subdirectory to URL parameter

I hope this was not asked over and over again before, but I didn't get further to an answer using google, w3schools and so on. So here is my question: I'm writing a script that creates kind of an index of projects that I have on my homepage and makes a nice list with images and teaser text based on an info file. I mostly have my projects on github and the readme is in markdown so I thought I could dynamically generate the HTML from the markdown of the latest blob from github on demand using PHP so it gets updated automatically. My directory structure looks like this:

projects
    project1
        .remoteindex
        .info
    project2
        .remoteindex
        .info
    index.php
    .htaccess

So when just domain.tld/projects/ is requested I get the info from .info and make a nice index of all projects. But if domain.tld/projects/project1/ is request it, I want to internally redirect it to domain.tld/projects/?dir=project1 to do my markdown parsing and so on. But domain.tld/projects/project1/image.png should not be redirected.

This is what I tried:

.htaccess
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]

I made it redirect instead of rewrite so that I can see what the error is because I just got an 404. The URL that I get redirected to is domain.tld/home/www/web310/html/projects/index.php?dir=project1 so obviously there is something going wrong with the internal structure of the web server an paths an whatever. I hope you can understand my problem an I would be very pleased if someone could help me, because I'm totally lost on .htaccess anyway.

Edit:

See my answer below for the used .htaccess.

The strange thing is that if I have an index.html in on of the subdirectories, my local web server (Apache with XAMPP for Mac OS X 1.7.3) does not rewrite and the index.html gets displayed, without one it works correctly.But on my real web server that serves my homepage it rewrites both with and without index.html (which is what I want). Any hints on that?

Answers


Thanks for all the help so far! You guys are just awesome!

I figured out that a symbiosis of both of your solutions works well for me:

RewriteEngine on
RewriteBase /projects
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [QSA,L]

Of course only without [R], this was my fault. (See my question edit for another question please).


You need to add a RewriteBase /projects to the htaccess. That way the redirect will work properly.

Edit:

RewriteEngine on
RewriteRule ^index.php - [L]
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]

Following what you ask, this is important not to do a redirect, but let the rewriterule modify internally the URL i.e. the RewriteRule should not end with [R,L] but rather [L] and maybe the "query string append" directive to keep what's after the ? so this should probably be [QSA,L].

Now here's how I'd do to avoid rewriting static files: if it's not a file then (and only then) test it:

# if it's not a file...
RewriteCond %{SCRIPT_FILENAME} !-f
# ... and it's a dir
RewriteCond %{SCRIPT_FILENAME} -d
# ... then rewrite it internally and stop further processing:
RewriteRule projects/([^/]+)(/?)$ index.php?dir=$1 [QSA,L]
And now two hints:

Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)


Need Your Help

Which data structure to use for keeping a list unique with insertion order intact

java data-structures collections

I need to add objects to a list (with List semantics) while keeping all objects in the list unique. I figured LinkedHashSet would do, but the "re-insert" clause breaks this:

How to download a file from a website in C#

c# download windows

Is it possible to download a file from a website in Windows Application form and put it into a certain directory?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.