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'm using WAMP coupled with setup an online Host. During my index.php, there's code to obtain page passed and inspections whether it is available(in database). Otherwise, display a mistake message. It really works fine.

Eg: http://mysite/contactus/

But it won't work basically make use of a a directory title as page_title within the URL. Eg: http://mysite/images/. This can display page not found error (ie. inspections database with no page found, so display "not found"). But it won't display images,css(linked file) within the page. Also, it shows http://mysite/images/?page=images in addressbar.

Like this, basically goto js folder which is often used to keep javascript files, above problem happens. So, issue is triggered if any subdirectory's title is passed as pagename.

How you can solve this ?

When http://mysite/images/ is provided, mod_rewrite is redirecting to http://mysite/images/index.php?page=images rather than http://mysite/index.php?page=images

Edit Please let me know how you can block hotlinking of files and directory, and redirect to index page or send some browser header error ?

I attempted 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 obvious the issue without exhibiting photos and css whenever a directory title is pointed out. But whatever pagename i specify eg:http://mysite/contactus, it'll goto URL: http://mysite/index.php?page=contactus. Even when I personally use a directory title eg: http://mysite/js, it'll goto: http://mysite/index.php?page=js

I'm very confused.

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

you need to place the slash in-front.


Edit: transformed the ? to *

My understanding is your script is perfect for documents only, not images or any other assets.

Then you definitely should ignore them immediately. Try adding this line immediately after RewriteBase such as this :

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

Then these subdirectories could be offered immediately, thus skipping the following RewriteRule set.

For that trouble with the sites It's my job to pressure a slash

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^\.]+[^/])$    $1/ [R]