Forum Moderators: phranque

Message Too Old, No Replies

Content Negotiation via MultiViews

Can you help me with syntax?

         

iaaa

11:04 pm on Apr 24, 2006 (gmt 0)

10+ Year Member



Hi,

I am trying to use content negotiation to make extensionless URLs. The website I am working on is static, HTML4.01Strict and I only use the following file types: .html, .png, .jpg, .txt, .rss. I am not interested in 'qs' or 'q' as I only got english content (no multilanguage). So, assuming that the website directory structure is as follows:

www.example.com
www.example.com/category1/
www.example.com/category2/
www.example.com/category3/
www.example.com/category3/subcat1/

etc etc

a) Will the following work? Notice the slash in the opening tag - not quite sure what that should be? I want the rule to apply throughout all sub directories (if possible!).

<Directory />
Options + MultiViews
</Directory>

b) My thinking is that I have to place the above code in the .htaccess in the root dir (which doesn't exist at the moment). I.e. www.example.com/.htaccess
Or do I have to place the code in every single directory and sub-directory?

Regards.

jdMorgan

2:40 pm on Apr 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A couple of points, here.

<directory> containers cannot be used in in .htaccess (see the Apache core documentation for <directory> context). However, <directory> is not needed for this application.

The correct syntax for adding the MultiViews option is:


Options +MultiViews

(Note no space after "+".)

For best results using Apache configuration directives, look up each directive in the Apache documentation [httpd.apache.org], and read the entire description. Then implment the directive using the exact syntax shown, to include spacing and character case.

The default behaviour for .htaccess files is that they will apply to the current directory, and any subdirectories of that directory. If your server is configured to defeat this default behaviour, then see RewriteOptions inherit in mod_rewrite.

Jim

iaaa

4:57 pm on Apr 25, 2006 (gmt 0)

10+ Year Member



I can't tell you how much time that saved me Jim!

I will definitely read the documentation and especially a library thread somewhere around here on regular expressions.

Not everything can go fine though - I use ws_ftp to upload files to the server and guess what - .htaccess file is not visible from there. So when I uploaded my brand new (or so I thought) .htaccess file there, nothing seemed to have happened. I froze. I remembered reading somewhere that that file in particular is not always visible. Fortunately it is only a few days that I got this domain so haven't setup anything else there (unless my webhost had the file there already with any directives - which of course I lost!). One thing I knew I had is the 301 from the non-www to www version of the domain - I replaced that and added the MultiViews directive as you told me. I uploaded that on the root and it now holds the following 5 lines:

Options +MultiViews
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Closed browser, clear cache, typed example.com and it redirects properly. Phew!

Content Negotiation seems to be working fine as well; I removed the .html and .png extensions I used in first instance and it works as it should. I.e.

http://www.example.com/website/about.html
becomes:
http://www.example.com/website/about

I still got a few questions though which I would be delighted to get a reply if you have some time:

a) Is content negotiation will work for all file types? For example can I also remove the .cgi extension from the scripts when I refer to them within the html? Obviously I can try it out and see what happens, but even if it worked, would it be right? Logically I should be able (and I should) remove all extensions when I am referring to them from within the html.

b) The <base> tag. I use relative linking on my website in the format "/website/about" (starting always from the root) but I also use the Base tag in which I specify the actual URL. Before enabling MultiViews, my Base tag would be:

http://www.example.com/website/about.html

Now, with Content Negotiation enabled, should I continue to use the above or should I also remove the extension from the <base> like in:

http://www.example.com/website/about

Logic says, again, that I should knock off the extension.

If the answer to both a) and b) is to remove the extensions that is indeed amazingly practical. I could keep the URIs in that format forever!

Regards.

iaaa

5:12 pm on Apr 25, 2006 (gmt 0)

10+ Year Member



Just to add something up.

If I type:
example.com/website/about

it redirects to

www.example.com/website/about.html (adds the extension)

While if I type:
www.example.com/website/about (including the www)

it resolves to

www.example.com/website/about

That can mean only one thing; the 301 needs further config. Why it doesn't catch both scenarios properly.

Regards