0

In my site when I go to URL such as my-site.com/css or my-site.com/js, then browser displays the folder listing instead of the actual page saved in the database. If I use Options All -Indexes in my htaccess then it says forbidden. I have following rule in my htaccess to load any dynamic page:

RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]

Since I have also folders named css and js in my project, so I think server displays the directory content instead of dynamic page. How can I prevent this conflict. I know if I use a suffix "page" before my page name then it can be sorted out but i don't want to use any parameter before the page url.

EDIT

Here's my complete htaccess file:

# my-site.com

Options +FollowSymlinks -MultiViews

#RewriteBase /

#Enable mod rewrite
RewriteEngine On

DirectorySlash off

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]

1 Answer 1

1

Since /js and /css are real directories your rewrite rules don't execute because you have this condition:

RewriteCond %{REQUEST_FILENAME} !-d

which means don't run for directories.

You can tweak your .htaccess to this:

Options +FollowSymlinks -MultiViews
RewriteEngine On

# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L,R=302]    

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?category=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?category=$1&post=$2 [QSA,L]
Sign up to request clarification or add additional context in comments.

4 Comments

though your effort is appreciated and it fixed the problems for pages such as css or js etc. but it has started to give me problem for homepage. I'm getting page redirection problem and a lot of ///////////////////// in address bar.
Make sure you don't have any other line in your .htaccess and you completely clear your browser cache
then i think it will work on live server but not on my localhost, isn't it?
I've tested these rules on my Apache and it works perfectly fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.