Content negotiation for extensionless URLs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdmcginn
    Junior Member
    • Mar 2004
    • 22

    #1

    Content negotiation for extensionless URLs?

    In the spirit of Tim Berners-Lee's article Cool URIs don't change, I'm interested in using content negotiation in my .htaccess file, so that I can leave off extensions from my URLs. For example, so I can use "mysite.com/about" instead of "mysite.com/about.htm"

    This was suggested for my .htacess file:
    Code:
    <Directory /home/user/public_html>
    Options + MultiViews
    </Directory>
    But it gives a Error 500 Internal Server Error.

    Website Optimization suggests two options: "Type maps contain paths to the variants of each MIME type to express a server-side preference for resources. The MultiViews option expresses no server-side preference and effectively fakes a type map file from searching the directory for files with MIME type extensions."

    Any other suggestions? Am I going to succeed in doing this?
  • Elite
    Senior Member
    • Apr 2004
    • 168

    #2
    You could try modrewrite (untested):

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.htm [L,QSA]

    Comment

    • ZYV
      Senior Member
      • Sep 2005
      • 315

      #3
      First I see an undeeded space, should be "+Multiviews" IMHO. And remove this <Directoty ...> wrapper.

      I've tried and

      Code:
      [b].htaccess[/b]
      Options +MultiViews
      works on cpanel16.

      P.S. mod_rewrite solution will work, but please avoid using mod_rewrite per-directory when it is not absolutely necessary, as it is a rather ressource-hungry Apache module.

      Comment

      • Elite
        Senior Member
        • Apr 2004
        • 168

        #4
        Originally posted by ZYV
        P.S. mod_rewrite solution will work, but please avoid using mod_rewrite per-directory when it is not absolutely necessary, as it is a rather ressource-hungry Apache module.
        Fair point

        Comment

        • mdmcginn
          Junior Member
          • Mar 2004
          • 22

          #5
          Thanks so much!

          I added the line "Options +MultiViews" to my .htaccess file. That got it working with URLS such as mysite.com/index/ (I had to add a slash at the end) but then my relative links stopped working. Adding <base href="http://mysite.com/" /> fixed that.

          Comment

          • ZYV
            Senior Member
            • Sep 2005
            • 315

            #6
            Glad you sorted it out

            Comment

            • mdmcginn
              Junior Member
              • Mar 2004
              • 22

              #7
              Not exactly. This leaves me with several URLs pointing to the same content (and Google doesn't like duplicate content).

              The mod_rewrite solution suggested by Elite gives error 500. But of course, he said it was untested. Once I removed the QSA flag, it works for mydomain.com/filename, and works for mydomain.com/filename.htm (so that's duplicate content). It gives error 500 for mydomain.com/filename/ (adding the slash) and mydomain.com/filename/more. To avoid duplicate content, I don't want those last two options to work anyway, but I think error 404 is better than 500 for this.

              My MultiViews htaccess file (below) allows all four URL patterns, giving me four cases of duplicate content!

              Code:
              ErrorDocument 404 /404.shtml
              AddHandler application/x-httpd-php5 .php .htm
              Options +FollowSymlinks
              Options +MultiViews
              AddDefaultCharset On
              So I need rules that will force only one URL pattern to work, probably mydomain.com/filename/ But I also want to force mydomain.com to redirect to www.mydomain.com (to maximize Google Page Rank and minimize duplicate content penalties). So that rewrite rule needs to work with any others.

              Comment

              • ZYV
                Senior Member
                • Sep 2005
                • 315

                #8
                Not sure that MultiViews solution will work in such a complex case, leaving you with the mod_rewrite option. Read the mod_rewrite guide and docs, everything is there. All you need is to make a final decision on which URLs you want to use and come up with several regexp's for RewriteRules.

                Comment

                • mdmcginn
                  Junior Member
                  • Mar 2004
                  • 22

                  #9
                  Here's the solution from http://forum.modrewrite.com/, assuming all my files end with html:

                  Code:
                  Options +FollowSymLinks -MultiViews
                  
                  RewriteEngine On
                  
                  # force www.
                  RewriteCond %{HTTP_HOST} .
                  RewriteCond %{HTTP_HOST} !^www\. [NC]
                  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
                  
                  # redirect *.htm, *.html and * (with no trailing slash)
                  # if a .html file exists to */
                  RewriteCond %{ENV:REDIRECT_STATUS} ^$
                  RewriteCond %{REQUEST_URI} ^(/[^\.]+)$ [OR,NC]
                  RewriteCond %{REQUEST_URI} ^(/.+)\.html?$ [NC]
                  RewriteCond %{DOCUMENT_ROOT}%1.html -f
                  RewriteRule .*[^/] %1/ [R=301,L]
                  
                  # */ --> *.html, internally
                  RewriteRule ^(.+)/$ /$1.html [QSA,L]

                  Comment

                  Working...