PDA

View Full Version : .htaccess



Dal
09-01-2008, 04:30 AM
Hi

Ive never really needed much to do with the .htaccess file and I dont know a great deal about it. :confused: However, I have built part of my site with allowance for friendly URL's. I use the method where by I drop the .php extension and use the '/' (forward slashes) as a separator in an array. Anyway, it worked localy with my apache server setup but when I uploaded my site to my host I found that the search page just shows all of my php instead of executing it. It also pends me to download my "search" file.

.htaccess file includes;


AddType x-mapp-php5 .php

<Files search>
ForceType application/x-httpd-php
</Files>



The first file was specified to be included if I wanted to use php5 instead of the default version 4. Ive tried with and without this line so I know it to be causing no problems.

The .htaccess file is contained in my root and the search file is within the same directory.

Anyone know what could be the problem?
Thanks
Dal

BLiZZaRD
09-01-2008, 06:41 PM
You have the type added but no handler. The basic one is as such:



AddType application/x-httpd-php5 .html .htm .php
AddHandler application/x-httpd-php5 .html .php .htm


Also, if you are just after friendly URLs, follow the examples here taken from B&T's Tips and Scripts site:



If you want your website to have friendly URLs you can use this .htaccess code. With this example code, the URL http://example.com/about would cause the page about.htm to load.

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_FILENAME}\.htm -f
RewriteRule ^(.*)$ $1.htm [L]

With the above example, if there is no dot in the requested name (this prevents executing the filename lookup logic for every file request), and the request matches a filename (with a .htm on the end), it will take the request and put .htm on the end.

There are two restrictions with this method. Make sure there is not a directory with the same name as the url you are using (if using http://example.com/about - make sure there is not a directory named about), or you may get undesired results. And the page you wish to load cannot have a dot in the name (i.e. about.me.htm will not work).

Here is a second method. This one does not do a filename check so it is faster, but it could also do a rewrite when there is no file.

RewriteEngine On
RewriteRule ^([^\./]+)$ $1.htm [L]

With the above example, if there is no dot or slash in the requested name, and the request is not empty (meaning the default page), it will take the request and put .htm on the end.

There are also two restrictions with this method. Make sure there is not a directory with the same name as the url you are using (if using http://example.com/about - make sure there is not a directory named about), or you may get undesired results. And the page you wish to load cannot have a dot or slash in the name (i.e. about.me.htm or sub/about.htm will not work).

You can make any number of variations as to the criteria using these examples as a starting point.

Dal
09-01-2008, 07:23 PM
Thanks BLiZZaRD :)

Im not too clued up on .htaccess as you may realise and I currently have a working script to take the url path and split it using the "/" forward slashes. The code I created works great (that wasnt me on my high horse, I mean that it just works well:)).

Im not sure how much control I have with my current host and I will certainly look into handling things differently when (or if) I get a dedicated server. Because Im a noob on servers and I love linux for its stability and its functionality (also because it's not M$) but I need a place to start with learning the foundation. I know this is now getting off topic but linux apache web foundation tutorials would be great if you or anyone else knows of any sites that would provide everything I need to know (at a beginner level)?

Thanks for the help.

ps my host eventually responded and they have suggested I use;

AddType x-mapp-php5 .php

<Files search>
ForceType x-mapp-php5
</Files>
<Files viewbizybox>
ForceType x-mapp-php5
</Files>

Would that be the same as what you suggested?

BLiZZaRD
09-02-2008, 02:29 PM
A great resource for .htaccess (and many other things) is B&T's Scripts (http://tips-scripts.com/)

And of course, you can always go right to the source (http://httpd.apache.org/docs/1.3/howto/htaccess.html)

What you have will work, but with that method you MUST end your files in a .php5 extension, instead of just .php ;)