By Matt

2011-12-14 23:22:48 8 Comments

The situation

Across the entire domain, we'd like the URLs to hide file extensions and remove trailing slashes, independent of the domain name itself (as in, works on any domain).

Sample of our directory structure

We're not using index.* files except for the homepage.

  • /
    • /index.php
    • /account.php
    • /account
      • /subscriptions.php
    • /login.php
    • /login
      • /reset-password.php

The goal

Some examples of how these files might be requested, and how they should look in the browser:

  • / and index.php --> (literally just the bare domain name).

  • /account.php or /account/ or /account -->

  • /account/subscriptions.php or /account/subscriptions/ or /account/subscriptions -->

As you can see, there are several ways to access each webpage, but no matter which of the 2 or 3 ways you use to get there, it only shows the one preferred URL in the browser.

The question

How is this done with .htaccess using mod_rewrite?

I've banged my head against the wall trying to figure this out, but in general, the rewrite flow would seem to be something like this:

  1. External 301 redirect ( --> )
  2. Internally append .php ( --> )

I've been Googling this all day, read thousands of lines of documentation and config texts, and have tried several dozen times... I think more brains on this would help a lot.


We found an answer our question (see below).


@Matt 2011-12-20 00:39:09

Thanks for your time to look at the question, but we appeared to have figured it out:

Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off

# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]

We have to turn off Multiviews and Indexes so the engine doesn't get confused and instead try to reference any index.* file or show a directory listing (also confusingly called an "index" with Apache...) when a directory appears to be requested.

The first redirect visibly (R=301) removes the trailing slash, and the second one internally rewrites it to the PHP (or HTML, etc.) file at hand.

This .htaccess file also supports query strings.

Update As noted in the comments below much earlier, we've switched to nginx, and this is all our conf file contains related to URL-rewriting (from my dev box):

location = / {
    index index.html;

try_files $uri $uri.html =404;

We've also switched from PHP to just plain HTML, but changing the extensions above should hardly make a difference, if at all.

@Matt 2012-07-31 12:36:08

In hindsight, we've switched to nginx and this whole task was SUPER-easy...

@MrWhite 2012-10-20 17:15:13

Minor point, but you don't need to escape the forward slash (/) in the regex, as it carries no special meaning. \/ is the same as '/'.

@Matt 2012-10-21 03:58:22

@w3d Good point. That's a habit from my Javascript regex'ing.

@Kevin C. 2011-12-16 00:38:22

If you would like a flexible and simple way to route URL requests for your website or app, I would recommend a PHP micro-framework. Not only will you have complete control over your URL routing, but it will provide other benefits as well.

I've used the Slim Framework before, combined with the Symfony Templating Component with great results. If I were to do it again, I would probably just use the Silex Micro-framework, since it's also a part of Symfony Components.

@Matt 2011-12-16 00:58:35

We thought about something like this (thanks for the links), but our development version of the site is PHP-powered and the deployed production site is 100% static (no PHP). This .htaccess file will be fore the deployed production site.

@Kevin C. 2011-12-16 07:48:38

It would be very easy to quickly create a static site based on these micro-frameworks. The project I used them for was mostly static content, with just a splash of PHP used to bootstrap the site using the micro-framework. Depending on the size of the site, converting it to use a micro-framework might be faster than banging your head against .htaccess all day.

@jcisio 2011-12-15 06:53:41

Why do you want to redirect /account to /account.php? In fact, the page /account only exists with Content negotiation. If you don't want it, just disable the directive.

About your two rules, I think it is straightforward:

RewriteRule ^/account/$ /account
RewriteRule ^/account$ /account.php [R,L]

It is untested, however. You can also add [R,L] to the first line, in this case, the browser will make one more redirect.

@Matt 2011-12-15 16:06:05

In theory, that's the general idea (except we don't want to display .php in the browser), but that would only work for account.php. We're hoping for a general solution that works throughout the site. Using refiddle to test our regex's, we know that regex's like ^(.*)/$ indicate URLs with a trailing slash, but that only seems to have effect in top-level directories, not subdirectories. (Requests to subdirectories re-write with full server path in the URL and show it in the browser for some reason.) We're also certain that ^(.*)[^/]$ indicates a URL without a trailing slash.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to remove subdirectory and index.php from URL with htaccess?

2 Answered Questions

[SOLVED] .htaccess redirect url without trailing slashes

2 Answered Questions

1 Answered Questions

[SOLVED] rewrite url - remove trailing / if any and add .php

2 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] How to remove a slash with .htaccess

2 Answered Questions

Sponsored Content