Forum Moderators: phranque

Message Too Old, No Replies

non-www and extension-less re-write

host messed with .htaccess

         

Tastatura

6:44 pm on Sep 14, 2008 (gmt 0)

10+ Year Member



Hi,
background - recently my host messed around with some server configuration which inadvertently affected my php scripts (while back they upgraded to php5 although php4 is still available AND that's what I am using until I have chance to comb through everything...). I called them up, "they resolved it", and everyone was happy. Now I noticed that my www to non-www redirects are not working as is not 'extension-less' page rewrite (and host support is clueless).
I also found comment and additional lines in my .htaccess file which did not make me very happy as they didn't tell me about it- they added "AddHandler application/x-httpd-php4 .php"[/b] and apparently deleted some stuff as I think I had "Options All -Indexes" in there. (I can't get to my backup at the moment...)
I went through .htaccess files but I can't figure it out, although I think it's something simple and staring me in the face).

.htacess is basicly there to redirect any www call to non-www, 'make' pages extension-less (they are .html with bunch of php includes), handle php files, and don't show directory index file name

As of now

.htaccess in my root is

# Use PHP4 as default
AddHandler application/x-httpd-php4 .php
addhandler application/x-httpd-php .htm .html
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule [^/]$ [%{HTTP_HOST}%{REQUEST_URI}...] [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/)*([^./]+)/?$ $2.html [L]

and .htaccess in my sub-folders (where indexfolder is replaced with actual folder index file name)


addhandler application/x-httpd-php .htm .html
DirectoryIndex indexfolder.html
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule [^/]$ [%{HTTP_HOST}%{REQUEST_URI}...] [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/)*([^./]+)/?$ $2.html [L]

I would appreciate another set of eyes looking at this as I am going blind.... :)

Tastatura

4:26 am on Sep 15, 2008 (gmt 0)

10+ Year Member



so coming back to this I am still stuck. I still can't get to my backup... I decided to start from scratch sort of speak.
-I completely deleted .htaccess file in subfolder and will try to handle everything (if possible) from root .htaccess file
-I've rewritten main .htaccess file, got www-to-non-www taken car of (i think :) ), but am having problems 'extensionless' part

When all is said and done, I would like to url to look like
http://example.com/test/testfile

so - www.example.com redirects to example.com/
but www.example.com/test/testfile.html redirects to example.com/test/testfile.html (.html is still visible)

and www.example.com/test/testfile redirects to example.com/test/testfile and gives 404 (proper redirect to non-www but can't 'find' the file)

This is what I got

Options All -Indexes
# Use PHP4 as default
AddHandler application/x-httpd-php4 .php
AddHandler application/x-httpd-php .htm .html
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/)*([^./]+)/?$ $2.html [L]

Any help is appricated

jdMorgan

3:39 pm on Sep 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Options All -Indexes
# Use PHP4 as default
AddHandler application/x-httpd-php4 .php
AddHandler application/x-httpd-php .htm .html
It looks like your internal rewrite will drop the "/test" from your example URL-path. The parentheses and back-reference number look wrong.

DirectoryIndex index.html
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) http://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^./]+)/?$ /$1.html [L]

I also modified the second RewriteCond so that it invokes the rewrite if the requested URL-path plus ".html" exists as a file, rather than invoking it if the extensionless request does not exist as a file. This gives files with .html extensions "priority" over extensionless duplicate files, but also saves needless rewriting if the rewritten URL-path won't exist. The new evaluation priority is "not a directory, does exist as an html file."

I also end-anchored your hostname pattern so that the hostname must be exactly "example.com" with no appended period and/or port number.

Note that in accepting an optional trailing slash, you create duplicate-content. It would be better to redirect if the slash is wrong in any case, rather than accepting either URL. Given the 'emergency' nature of your problem, I doubt you're concerned with that right now... :)

Jim

[edited by: jdMorgan at 3:39 pm (utc) on Sep. 15, 2008]

Tastatura

3:40 pm on Sep 16, 2008 (gmt 0)

10+ Year Member



Thanks JD! I really appreciate the help!

g1smd - I also appreciate the effort as well (your inbox is full so I couldn't reply ;) )