Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

Edit:
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]

##
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

update

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).

share|improve this answer
    
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." –  chanchal1987 Dec 12 '10 at 14:33
    
Updated, how about this one? –  Stephan Muller Dec 12 '10 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. –  chanchal1987 Dec 13 '10 at 16:07
    
Ah, edited once again. –  Stephan Muller Dec 13 '10 at 16:09
    
This is also throwing a Server error! I updated my question with my current .htaccess file. –  chanchal1987 Dec 13 '10 at 21:08
add comment

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]
share|improve this answer
add comment

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.