Forum Moderators: phranque

Message Too Old, No Replies

move .htaccess to httpd.conf server wide canonical & extensionless.

         

Larose

4:38 am on Apr 23, 2011 (gmt 0)

10+ Year Member



Hi, I need help about server wide configuration. I write this post to be as complete as possible as to be a reference to people looking for similar solution.

Myself: Been searching for many months this solution, 100+ hours.

Situation: I have a dedicated server but not for resell, it is for my webmasters to manage my websites.

I wish to have server wide configuration and then prohibit the use of .htaccess to my webmasters. For security purpose, but mostly for performance.

It is recommended by apache documentation to use httpd.conf over .htaccess for performance issue and this is what I want.

I do it through the include editor in apache configuration so that my modifications are kept when there is an update of version. This is the official way recommended. But few seems to do it this way.

In the "Pre VirtualHost Include" I have inserted my .htaccess and appart from including the Directory, the syntax is the same.

Now, I want my .htaccess for a website to be generic and apply theses rules to every websites on the server.

First, Is it sane to do this?

Second, I managed to do it server wide for not using www this way:
<Directory /home/*/public_html>

RewriteEngine On
Options -Indexes +FollowSymLinks

# redirect www to no-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule .? [%1%{REQUEST_URI}...] [R=301,L]

</Directory>

I have not seen this anywhere on the web yet.
All my websites and sub-domains and now free of www.

Now I wish to have extensionless also server wide.

here is the code I use successfully for a domain:

###Remove File Extension From URL - BEGIN
IndexOptions FancyIndexing
Options All
# Externally redirect client requests contains htm/html extension to
# extensionless URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.html?
# Externally redirect to extensionless URI
RewriteRule ^(([^/]+/)*[^.]+)\.html?$ [yourwebsite.com...] [R=301,L]
# If requested extensionless URL-path does not resolve to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# If requested extensionless URL-path plus ".html" does resolve to an existing file
RewriteCond %{REQUEST_FILENAME}.html -f
# then append ".html" to resolve the actual filename
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [L]
###Remove File Extension From URL - END

How can I make this server wide without adding a website adress but a generic rule for all domains?

Then I would add:
# Prohibit the use of .htaccess
AllowOverride None

I think this would be the best performance/security solution but I need help & advise because it is too much for me alone. I have been searching all over but could not find infos about this situation. Thank you so much for your help!

jdMorgan

5:30 pm on Apr 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the variable "%{HTTP_HOST}" instead of the hard-coded hostname in your "externally redirect to extensionless URL" rule, similar to how you used it in your "remove www" rule.

Note that there is no need to check for "directory does not exist" unless you want to allow directories to be accessed, and further, that you want to allow them to be accessed without typing their trailing slash. If neither of these are true, then removing the directory-exists check will reduce disk reads and speed up your server a little.

Jim

Larose

11:07 pm on Apr 26, 2011 (gmt 0)

10+ Year Member



Hi, yes I have tried using the variable "%{HTTP_HOST}" and other variants. Here is what it does:

"http://website.com/file.html"

will rewrite to:
"http://website.com/home/user/public_html/file"

It is adding "/home/user/public_html"

I was thinking of setting the Location too.
I am not an expert but I am reading a lot about this and until now have not the correct syntax.

You were right about the condition or "directory does not exist" I got rid of it.

Thank you for your help.