I want to implement a rewrite rule in my .htaccess file to show a specific url as a directory of my server. See the code below I written,

RewriteRule ^(.*)/$ ?page=$1 [NC]

This will rewrites urls like www.mysite.com/abc/ to www.mysite.com/index.php?page=abc.
But if I request www.mysite.com/abc then it is throwing an 404 error.
How can I write a rewrite rule which will match www.mysite.com/abc and www.mysite.com/abc/ both?

My current .htaccess file (After Litso's answer's 3rd revision) is like below:


ErrorDocument 401 /index.php?error=401
ErrorDocument 400 /index.php?error=400
ErrorDocument 403 /index.php?error=403
ErrorDocument 500 /index.php?error=500
ErrorDocument 404 /index.php?error=404

DirectoryIndex index.htm index.html index.php

RewriteEngine on
RewriteBase /
Options +FollowSymlinks
RewriteRule ^(.+)\.html?$ $1.php
RewriteCond !-d
RewriteRule ^(.*)/$ ?page=$1 [NC,L]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ ?page=$1 [NC,L]


2 Answers 2



RewriteCond !-d
RewriteRule ^(.*)/$ ?page=$1 [NC,L]

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ ?page=$1 [NC,L]

This first rewrites urls with a slash, but only if it's not an existing directory.

If that's not found it rewrites urls without a slash (but only if it's not index.php, so already rewritten urls are ignored).

  • This is throwing an Internal Server Error. Error Details: "Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request." Dec 12, 2010 at 14:33
  • Updated, how about this one? Dec 12, 2010 at 15:30
  • It is working fine. But another problem occurs. It also rewrites my existing directories like img, css etc., and also sending an 404 Not Found error while I am trying to access index.php in my root directory. Dec 13, 2010 at 16:07
  • Ah, edited once again. Dec 13, 2010 at 16:09
  • This is also throwing a Server error! I updated my question with my current .htaccess file. Dec 13, 2010 at 21:08

The following conditions and the rule should do the trick for you. You can chain RewriteCond directives with logical AND (default) or logical OR (by appending [OR]).

# Check that there's no directory with that name
RewriteCond !-d
# Check that there's no regular file with that name
RewriteCond !-f
# Prevent loop if index.php is requested
RewriteCond %{REQUEST_URI} !index.php
# Finally rewrite the request; trailing slash optional
RewriteRule ^(.*)/?$ index.php?page=$1 [NC,L]

Your Answer

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

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