Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am using the following .htaccess file to keep my URL's clean

RewriteEngine On
RewriteRule ^([a-zA-Z\-0-9]+)/?$ index.php?get1=$1 [B]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?get1=$1&get2=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?get1=$1&get2=$2&get3=$3

It works so far, but I now want to access a directory.

For example. If someone goes to mysite.com/news/post-title, the URL is rewritten and everything is ok.

Now I want to have a directory that can be viewed when someone goes to mysite.com/directory and have it's contents shown.

Is there a way to do that?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Put a RewriteCond in front of each RewriteRule, to force the RewriteRule to be skipped if the request is made to a location that's a directory and exists.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z\-0-9]+)/?$ index.php?get1=$1 [B]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?get1=$1&get2=$2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?get1=$1&get2=$2&get3=$3

All that's going to get you is the actual directory listing. If you want to allow the users to also access the files within those directories, you should also add a condition to each of those checking for file existence:

RewriteCond %{REQUEST_FILENAME} !-f
share|improve this answer
    
while googling for an answer, i came across RewriteRule ^(dir_name|file_name\.extension|and_so_on) - [L]. Seems to get things done. Is it worse? Am I taking some risk by using it? –  AnPel May 21 '12 at 21:55
    
@AnPel That will also work, but requires you to have a known list of locations. If you've only got a single specific location that you need to account for, that will work great! –  Shane Madden May 21 '12 at 22:08

I would go for something like the following;

<Location /Somedirectory>
    Options Indexes
</Location>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^/Somedirectory - [L]

RewriteRule ^([a-zA-Z\-0-9]+)/?$ index.php?get1=$1 [B]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?get1=$1&get2=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?get1=$1&get2=$2&get3=$3
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.