homepage Welcome to WebmasterWorld Guest from 54.161.236.92
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 / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
htaccess files directive for parsing html as php
Having problems with files directive in htaccess for parsing html as php
MrBlack




msg:3895430
 3:10 am on Apr 19, 2009 (gmt 0)

I want to parse my root index.html file as php.

My site structure is like this:

www.example.com/index.html
www.example.com/sub-directory/index.html
www.example.com/sub-directory/sub-sub-directory/index.html

The problem I am faced with is that I want to restrict the parsing to just the index.html in the root of my website and not the index.htmls in the subdirectories of my site.

I have the following line in my htaccess which is in the root of my site:

<Files index.html>
AddType application/x-httpd-php .html
</Files>

Is this gonna do the trick? Or is it going to parse all index.htmls in my site? If so, does anyone have the correct directive I can use?

Thanks

 

rocknbil




msg:3895590
 3:36 pm on Apr 19, 2009 (gmt 0)

That might do it, but you're parsing all html files as php. .htaccess works per directory, so it would just apply to root files.

If you're only applying it to one file, can you do mod_rewrite? This might work better:

RewriteEngine On

RewriteRule ^(index.html*)$ /yourscript.php [L]
RewriteRule ^$ /yourscript.php

Some notes:

the * means "zero or more of the preceding character" so it will also work for requests for index.htm

The second rule means "begins and ends with nothing" so will manage requests for example.com/ in addition to example.com/index.html

If you want to pass parameters to the script,

index.html?id=1234

Do this:

RewriteRule ^(index.html*)$ /yourscript.php?%{QUERY_STRING} [L]
RewriteRule ^$ /yourscript.php?%{QUERY_STRING}

yourscript.php should receive id=>1234 in $_GET/$_REQUEST

Note I am not a mod_rewrite expert, there may be more accurate solutions, but I use these and they work without issues.

MrBlack




msg:3895610
 4:22 pm on Apr 19, 2009 (gmt 0)

Thanks for the reply rocknbil, but I just ran a test and the htaccess as I have it is also parsing the index.htmls in subdirectories as php.

rob7591




msg:3895677
 7:38 pm on Apr 19, 2009 (gmt 0)

Yeah, I believe .htaccess works per-directory with subdirectories. So if you put it in /sub1, it wouldn't work for /, /sub2, /sub3, etc, but it would work with /sub1/sub2, /sub1/sub3, etc.

Anyway, If you only want to do the index.html script, you should use a RewriteRule like rocknbil suggested. I don't know if there's a way to limit the rules to ignore subdirectories. I couldn't find one from Google.

rocknbil




msg:3896247
 4:40 pm on Apr 20, 2009 (gmt 0)

I just ran a test and the htaccess as I have it is also parsing the index.htmls in subdirectories as php.

Sorry, I may have confused you, and am not extremely familiar this THAT approach. What you're doing there is adding the directive to parse HTML as PHP, and as you discovered, this probably affects all directories under it. As mentioned, if you use the mod_rewrite directive, it will only affect the directories containing that .htaccess.

MrBlack




msg:3896257
 4:57 pm on Apr 20, 2009 (gmt 0)

Thanks for the replies guys.

In the end I just renamed index.html to index.php. Since the site has been live I have been 301ing index.html/index.php to root so I guess and hope that the search engine bots will not detect any changes anyway.

jdMorgan




msg:3896267
 5:14 pm on Apr 20, 2009 (gmt 0)

As long as you have "index.php" in your list of index files in a DirectoryIndex directive (in your .htaccess file, or defined in the server config), the redirect to index.php to "/" should have no negative consequences for search. One more caveat, though: If you reatin this redirect, make sure you are checking THE_REQUEST to be sure that "/index.php" is being requested directly by the client before invoking the redirect. If you redirect when index.php is being requested as a result of an internal rewrite or the action of DIrectoryIndex, then you will get an 'infinite' loop.

BTW, the code posted above is redundant and has several other issues. Both rules can be replaced by one, "?" should have been used instead of "*", and it is not necessary to 'handle' the query string, which will pass through unaffected if no "?" is used in the substitution:

# Rewrite requests for "/index.html", "/index.htm", and "/" to /yourscript.php
RewriteRule ^(index\.html?)?$ /yourscript.php [T=application/x-httpd-php,L]

Note also the "T=" flag to force the MIME type.

Jim

MrBlack




msg:3896403
 7:56 pm on Apr 20, 2009 (gmt 0)

As long as you have "index.php" in your list of index files in a DirectoryIndex directive (in your .htaccess file, or defined in the server config), the redirect to index.php to "/" should have no negative consequences for search.

My site is hosted on a shared server so I have no way of knowing if index.php is in the list of index files in the server config.

But this is default behaviour for a apache web server isn't it?

jdMorgan




msg:3896441
 8:35 pm on Apr 20, 2009 (gmt 0)

If you can request "/" and get the contents produced by index.php, then you can assume that the server config already has index.php declared in the DirectoryIndex list. If not, then you can add it in your .htaccess file.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
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