Forum Moderators: phranque

Message Too Old, No Replies

.htaccess - is this working properly?

Need some expert input thanx.

         

Thor_X

12:46 pm on May 21, 2005 (gmt 0)

10+ Year Member



Hi,

I've just changed my site from old ugly subdomain.domain.com/index.htm to subdomain.domain.com/index/ which is forwarded to template.php. Base is set to ht*p://subdomain.domain.com/ so all links are relative. Everything works as intended except that somewhere along this process the load times have increased and I'm not sure where the culprit lies so I thought I'd start by asking here if the mod_rewrites I've done look kosher.

The following should be taken care of the in .htaccess.

  • redirect subdomain.domain.com & subdomain.domain.com/ to subdomain.domain.com/index/
  • redirect subdomain.domain.com/index to subdomain.domain.com/index/
  • redirect subdomain.domain.com/index/ (excluding switcher.php) to /template.php?page=$1/

    RewriteEngine On
    RewriteBase /
    RewriteRule ^$ http://subdomain.domain.com/index/ [R=301,L]
    # fix missing trailing slash
    # -> the file has not an extension
    RewriteRule!\..{3,4}$ - [C]
    # -> and does not end up with a slash
    RewriteCond %{REQUEST_URI}!^.*/$
    # => redirect
    RewriteRule ^(.+)$ /$1/ [R=301,L]
    RewriteCond %{REQUEST_URI}!^(switcher\.php)$
    RewriteRule ^([a-z0-9_/-]+)$ /template.php?page=$1 [NC,QSA,L]

    Hopefully I haven't missed anything essential. Any and all input on the matter will be greatly appreciated. If all this checks out I guess I'll be heading over to the php forum. :)

    Cheers, Pontus

  • jdMorgan

    4:09 pm on May 21, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Thor_X,

    Wemlcome to WebmasterWorld!

    I don't see anything terribly wrong, except for a possible deadloop in the last rule. Some other minor tweaks may result in minor performance improvements:


    RewriteEngine On
    RewriteBase /
    RewriteRule ^$ http://subdomain.example.com/index/ [R=301,L]
    # fix missing trailing slash
    # -> If the file has no extension
    [b]RewriteCond %{REQUEST_URI} !\..{3,4}$[/b]
    # -> and does not end up with a slash
    RewriteCond %{REQUEST_URI} [b]!/$[/b]
    # => redirect to add trailing slash
    RewriteRule ^(.+)$ [b]http:[i][/i]//subdomain.example.com[/b]/$1/ [R=301,L]
    # Internally rewrite all except template and switcher to template.php
    RewriteCond %{REQUEST_URI} [b]!^(template¦[/b]switcher)\.php$ [b][NC][/b]
    RewriteRule ^([a-z0-9_/-]+)$ /template.php?page=$1 [NC,QSA,L]

    In an .htaccess context, mod_rewrite behaves as if it is recursive, so the deadloop in the last rule must be explicitly prevented. This is because after a rewrite takes place in .htaccess, control is passed back to httpd.conf, and then to all .htaccess files in the new URL's local URL-path in order to check for further rewrites and/or access restrictions. So, it is possible that template.php is being rewritten to template.php until the server's internal redirection limit is reached (check your error log file).

    By explicitly preventing requests for template.php from being rewritten, this deadloop can be avoided.

    Change all broken pipe "¦" characters to solid pipe characters (usually Shift-\) before use.

    Jim

    Thor_X

    11:43 pm on May 21, 2005 (gmt 0)

    10+ Year Member



    Thanks for the welcome and reply Jim. Eager to try it out I did a classic copy/paste - save - upload - refresh. :rolleyes: Unfortunately changing the dummy URL:s didn't help either.

    I'll be dissecting the changes later on to hopefully learn something new. That's my preferred way of learning; trying - succeeding - perfecting.

    Unfortunately my host doesn't supply errorlogs and after reading up on the subject I can see why. Guess I'll have to go back to trying to configure my local server. Feels like I'm opening up Pandora's Box here.

    I have come up with a few other questions where a yes/no will be more than enough. Not afraid of digging in but sometimes it's nice to get a general idea.

    1. My unix-path in this case is /customers/dindomän.se/dindomän.se/httpd.www/ftg/, does that mean that every request will have to go through every directory?

    2. My template.php is using 4 require_once's (2 php and 2 htm-files) are they also going through the .htaccess?

    3. If yes on the above question, I'm guessing it should be enough to exclude them from the last rewriterule? (If it is indeed a performance issue I'll look into a different structure.)

    4. Is it possible to implement the same unix-path structure virtually on my local server? Would make it easier to see the performance bit I guess.

    I'm sure I had more questions before I sat down to type it. :lol: Thanks again.

    Pontus

    jdMorgan

    1:49 am on May 22, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    1. My unix-path in this case is /customers/dindomän.se/dindomän.se/httpd.www/ftg/, does that mean that every request will have to go through every directory?

    No, only the path from Document_Root -the path accessible via HTTP- is traversed.

    2. My template.php is using 4 require_once's (2 php and 2 htm-files) are they also going through the .htaccess?

    No php requires access resources through the filesystem, not by using HTTP connections.

    3. If yes on the above question, I'm guessing it should be enough to exclude them from the last rewriterule? (If it is indeed a performance issue I'll look into a different structure.)

    --See above--

    4. Is it possible to implement the same unix-path structure virtually on my local server? Would make it easier to see the performance bit I guess.

    Yes, if I understand the question.

    Jim