homepage Welcome to WebmasterWorld Guest from 54.205.99.71
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Is my .htaccess file optimized?
hiccupsheadache




msg:4428747
 7:31 pm on Mar 13, 2012 (gmt 0)

Hello all.

My site gets a lot of visitors - and i want to make sure my htaccess file isnt slowing down my server.

Can someone please tell me how i can make my htaccess file faster - or if it is as good as it can be?


Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteRule ^ - [F]
#
# Define canonical hostname (add "www" if it is missing)
RewriteCond www.%{HTTP_HOST} ^(www\.)?(www\.[a-z-]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^ - [E=MyHostName:%2]
#
# Externally redirect to add missing trailing slash to requested
# URLs containing no period in the final URL-path-part
RewriteRule ^(([^/]+/)*[^.]*[^./])$ http://%{ENV:MyHostName}/$1/ [R=301,L]
#
# Externally redirect to canonical "www" subdomains
RewriteCond %{HTTP_HOST} !^(www\.[a-z-]+\.[a-z]{2,6}\.?(:[0-9]+)?)?$ [NC]
RewriteRule ^(.*)$ http://%{ENV:MyHostName}/$1 [R=301,L]
#
ErrorDocument 404 /404.php



One more question...

My htaccess file stops my subdomains working - please can someone advice on the line to make my de.domain.com subdomain work?

Thanks so much for the help everyone! :)

 

hiccupsheadache




msg:4429401
 8:26 am on Mar 15, 2012 (gmt 0)

Hi guys,

I think i need to add the following...


# Stop mod_rewrite processing if "es" subdomain requested
RewriteCond %{HTTP_HOST} ^es\.mydomain.com
RewriteRule .* - [L]


But then this would stop the trailing slash mod working :-(

As i would still like to have es.domain.com/page/

with the trailing slash.

Thanks again for the help! :)

g1smd




msg:4429403
 8:41 am on Mar 15, 2012 (gmt 0)

You should order your redirecting rules from most specific to most general so the non-www/www rule is usually the last of the redirecting rules.

lucy24




msg:4429693
 8:18 pm on Mar 15, 2012 (gmt 0)

The URLs that require trailing-slash rewriting: Are they real directories or made-up addresses? I'm assuming made-up, but let's not miss the obvious.

hiccupsheadache




msg:4429817
 8:09 am on Mar 16, 2012 (gmt 0)

Thanks for the replies guys :-)

Yes - all directories are real. The only reason i'm adding the / and the www -- is so all backlinks go to the same pages.

g1smd - i tried changing the order - but any changes i make to my htaccess file stops it working. I don't know the code well enough to change it :(

The way the code is working right now is perfect - i just don't know if it would be better performance to change all the {ENV:MyHostName} -- to the actual domain.com

AND i would like to be able to use subdomains - which this htaccess file is blocking.

Thanks again for the help guys! :-)

lucy24




msg:4429820
 8:31 am on Mar 16, 2012 (gmt 0)

Huh. I assumed you would say no, they're pseudo-directories for SEO and/or user-friendliness. If they're real directories, why does the redirect have to happen in mod_rewrite instead of letting mod_dir take care of it?

hiccupsheadache




msg:4431260
 12:19 pm on Mar 20, 2012 (gmt 0)

Thanks for the reply lucy24, but i'm sorry - i don't know what you mean :(

Are you saying that because they are real directories - the "/" will be added automatically?


Could i change my htaccess file to this...

And www will be added, and / will be added?


Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteRule ^ - [F]
#
# Define canonical hostname (add "www" if it is missing)
RewriteCond www.%{HTTP_HOST} ^(www\.)?(www\.[a-z-]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^ - [E=MyHostName:%2]
#
# Externally redirect to canonical "www" subdomains
RewriteCond %{HTTP_HOST} !^(www\.[a-z-]+\.[a-z]{2,6}\.?(:[0-9]+)?)?$ [NC]
RewriteRule ^(.*)$ http://%{ENV:MyHostName}/$1 [R=301,L]
#
ErrorDocument 404 /404.php


I've been trying since i made this post now - and i don't feel like i'm getting any closer! the language used in a htaccess file is so complicated to understand! lol.

My subdomains goes to a page cannot be displayed -- and every little change to the existing code - stops the site working completly. :)

phranque




msg:4431283
 1:08 pm on Mar 20, 2012 (gmt 0)

Are you saying that because they are real directories - the "/" will be added automatically?

if you have mod_dir installed and haven't overridden the default DirectorySlash On setting then yes - it will automatically redirect to the trailing slash url.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved