My code:

```
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]

```

I am using WAMP and had setup a Virtual Host.
In my index.php, there is code to get page passed and checks if it exists(in database). If not, display an error message. It works fine.

Eg: `http://mysite/contactus/`

But it will not work if I use a a directory name as page\_name in the URL. Eg: `http://mysite/images/`. This will display page not found error (ie. checks database and no page found, so display "not found"). But it will not display images,css(linked file) in the page. Also, it shows `http://mysite/images/?page=images` in addressbar.

Like that, if I goto `js` folder which is used to store javascript files, above problem occurs. So, problem is caused if any subdirectory's name is passed as pagename.

How to solve this ?

When `http://mysite/images/` is supplied, mod\_rewrite is redirecting to `http://mysite/images/index.php?page=images` instead of `http://mysite/index.php?page=images`

**Edit**
Please tell me how to block hotlinking of files and directory, and redirect back to index page or send some browser header error ?

I tried this:

```
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) http://%{HTTP_HOST} [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]

```

**Edit**
New code(semi-working):

```
RewriteEngine on
RewriteBase /

# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [R,L]

RewriteRule ^article/([a-z]+)/?$  http://%{HTTP_HOST}/index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=$1 [L]

```

This code will clear the problem with not displaying pics and css when a directory name is mentioned. But whatever pagename i specify eg:`http://mysite/contactus`, it will goto URL: `http://mysite/index.php?page=contactus`. Even if I use a directory name eg: `http://mysite/js`, it will goto: `http://mysite/index.php?page=js`

I am very confused.