Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Apache to serve a blog which consists of static HTML files. Currently, the blog uses a pretty standard URL structure, like this:


which maps to the file


using Apache's mod_dir.

I'd like to remove the trailing slash so that URLs like


work in the same way (and don't get redirected). Is there a way to do that with Apache?

(Note that I want URLs with the trailing slash to continue to work, as well.)

(Further note: I saw something about Apache's DirectorySlash directive, but I don't think it does what I want … although I'm not sure about that.)

share|improve this question
up vote 6 down vote accepted

You could always use mod_rewrite to redirect the directory name without the trailing slash to dirname/index.html. You could use RedirectConds to make sure that redirection doesn't get done if the URL ends with a trailing slash or with .html, and that it only applies specifically to blog post URLs.

Let me whip up an example, this'll take a moment.

# Trailing slashes and .html suffix
RewriteCond !/$
RewriteCond !\.html$

# Check if it's actually a dir and if index.html exists
RewriteCond %{REQUEST_URI} -d
RewriteCond %{REQUEST_URI}/index.html -f

# Rewrite anything that gets through (Probably insecure, but you get the idea)
RewriteRule ^(.*)$ $1/index.html

Edit: Can also be combined with Matt's solution of adding the redirect error code to the RewriteRule. It should probably also be made the last RedirectRule. Refer to the mod_rewrite documentation for more.

share|improve this answer
RewriteCond: bad argument line '!/$' – Michael Oct 9 '14 at 22:07
@Michael Newer versions of Apache need RewriteCond %{REQUEST_URI} !/$, as well as RewriteCond %{REQUEST_FILENAME} -d and RewriteCond %{REQUEST_FILENAME}/index.html -f. The !\.html$ should also be changed to RewriteCond %{REQUEST_URI} !\.html$, but I don't think this adds much to the rule. Also, don't forget to turn off the default mechanism which adds the slash in the first place: DirectorySlash Off and to enable mod_rewrite (a2enmod rewrite + RewriteEngine on). – Georges Dupéron Feb 25 at 12:42
@GeorgesDupéron "Newer versions of Apache need..." that's all versions of Apache! ;) – w3d Mar 13 at 1:58

The Apache Rewrite Guide has a chapter on the "Trailing Slash Problem" (scroll down a bit) explaining how to solve the issue with the trailing slashes in general.

However, they also state that the trailing slash is required for directories when the rendered page in it uses resources (images, stylesheets, ..) with relative links - which will not work without the slash:

"The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!"

share|improve this answer

Removing index.php from URL can be done by writing only two lines in your .htaccess (mod_rewrite in Apache) file. Before writing the rules in .htaccess, make sure that mod_rewrite is enabled (RewriteEngine On) in your Apache server. Most probably mod_rewrite is enabled in a Linux server, but for a windows server you might need to contact the hosting people to enable mod_rewrite. You can check this by looking in phpinfo().

Below are the rules which will remove index.php from the URL:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)*index.php HTTP/ 
RewriteRule ^(([^/]+/)*)index.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]

Redirect 301 means "Moved Permanently" so most search engines will remove index.php from the URL.

share|improve this answer

Did you read the definition of the DirectorySlash?

The DirectorySlash directive determines, whether mod_dir should fixup URLs pointing to a directory or not.

Typically if a user requests a resource without a trailing slash, which points to a directory, mod_dir redirects him to the same resource, but with trailing slash for some good reasons:

  • The user is finally requesting the canonical URL of the resource
  • mod_autoindex works correctly. Since it doesn't emit the path in the link, it would point to the wrong path.
  • DirectoryIndex will be evaluated only for directories requested with trailing slash.
  • Relative URL references inside html pages will work correctly.

Well, if you don't want this effect and the reasons above don't apply to you, you can turn off the redirect with:

# see security warning below!
<Location /some/path>
DirectorySlash Off
SetHandler some-handler
share|improve this answer
Yes, but that's not the whole story. If you have a file like /some-dir/index.html and request /some-dir (no trailing slash) then you'll receive a 404 page in response. This can be fixed with rewrites, but I wondered if there was an option to do it automatically. (Also, geez, no need to be a smart ass about it.) – mipadi Mar 29 '11 at 18:22

I had the same problem, but I was using mod_alias to set up a subsite. Turns out, I needed to make a second alias without the trailing slash so that it would work correctly. Looked something like this:

Alias /forum/ "/var/www/forum"
Alias /forum "/var/www/forum"

<Directory "/var/www/forum">
    Options FollowSymlinks
    AllowOverride None
    Order allow,deny
    Allow from all

In Ubuntu, I had to edit the /etc/apache2/mods-enabled/alias.conf file with these lines, then restart apache. Couldn't find this answer anywhere on the web; I just stumbled onto it myself as mod_rewrite wasn't working and the DirectorySlash command didn't help either. I was appending a non-Drupal program as a subsite under a Drupal installation, which is what kicked off all this madness in the first place...

share|improve this answer

You might want to try an Alias:

share|improve this answer

Your Answer


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.