Forum Moderators: phranque

Message Too Old, No Replies

Remove file extensions but not block directories of same name?

Questions about .htaccess

         

NyahK

12:31 am on Aug 3, 2010 (gmt 0)

10+ Year Member



Hello, this is my first post on this site. Hopefully someone can help me out!

I would like to accomplish two things with my .htaccess file:

  • Remove file extensions (.php in this case).
  • Not prevent files and directories from having the same name.

    So for example, I need both URLs to work:

    http://www.example.com/pictures(.php)
    http://www.example.com/pictures/skiing(.php)

    With the code I'm using, /pictures(.php) doesn't work because the /pictures/ directory exists. It won't let me use file names which have directories of the same name! I know it's possible, though, because the content management systems of software such as IP.Board allows it, so...

    Can anyone help me out? Please? Here's what I'm using (there's also code in there to force www in the URL):


    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]

    Options -Indexes


    Please keep it as simple as possible!

    Thank you.
  • jdMorgan

    1:09 am on Aug 3, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Try this first:

    RewriteEngine On
    RewriteBase /
    #
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*[^/])$ /$1.php [L]


    Then speed it up significantly:

    RewriteEngine On
    RewriteBase /
    #
    RewriteCond $1 !\.(php|gif|jpe?g|png|ico|css|js)$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*[^/])$ /$1.php [L]

    Excluding the listed filetypes prevents the server from having to go do the two disk checks for requested objects which cannot be created/generated by your php scripts. You may add more filetypes if you like, but it's most important to exclude the most frequently requested ones -- you don't have to build an exhaustive list.

    Jim

    NyahK

    2:32 am on Aug 3, 2010 (gmt 0)

    10+ Year Member



    Thank you, Jim!

    Unfortunately, using that code, if I go to /pictures(.php) it loads the /pictures/ directory instead. Any way around this? As I said before, IP.Board does it so I know it's possible to have a PHP file and directory share the same name and still function.

    Here's hoping!

    youfoundjake

    3:09 am on Aug 3, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Jim wrote this out over a year ago helping me out..

    IndexOptions FancyIndexing
    Options All
    RewriteEngine on
    #
    # Externally redirect client /index page requests to "/"
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html?
    RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]
    #
    # 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?$ http://www.example.com/$1 [R=301,L]
    #
    # Externally redirect non-blank non-canonical hostname requests to canonical hostname
    RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]
    #
    # If requested extensionless URL-path does not resolve to an existing directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # and if requested extensionless URL-path plus ".htm" does resolve to an existing file
    RewriteCond %{REQUEST_FILENAME}.htm -f
    # then append ".htm" to resolve the actual filename
    RewriteRule ^(([^/]+/)*[^./]+)$ /$1.htm [L]


    I subsituted .htm for php as needed..
    [webmasterworld.com...]

    NyahK

    7:15 am on Aug 3, 2010 (gmt 0)

    10+ Year Member



    This code produces the same error as described in my second post, unfortunately.

    jdMorgan

    2:00 pm on Aug 3, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Sorry... Remove the
     RewriteCond %{REQUEST_FILENAME} !-d 

    line completely, or comment it out.

    Jim

    NyahK

    2:05 pm on Aug 3, 2010 (gmt 0)

    10+ Year Member



    Perfect!

    Thank you very much, Jim! :)