Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Problem removing .php extension with mod_rewrite

    Hey all,

    I want the URL http://domain.com/myfile to access http://domain.com/myfile.php

    I figured the following rewriteRule would do it:
    Code:
    RewriteRule ^([^/]+)/?$ $1.php
    In my head it should grab everything up to the slash, with a slash being optional, and get that grabbed stuff with a .php ending, however, I get a 500 error.

    So, I tried the following 2 possiblities:

    Code:
    RewriteRule ^([^/]+)$ $1.php  # type domain.com/filename into the browser
    RewriteRule ^([^/]+)/$ $1.php  # type domain.com/filename/ into the browser
    Oddly enough, the first of these two options results in a 500 error, and the second works fine.

    Does anyone know what I'm doing wrong here?

    Thanks.
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Its greedy greedy. I've never used it for something so simplistic before.
    Here's the problem. Damn, I just lost it. Oh well.
    Here's how to fix it, be more specific on the characters you allow. The last one works since you're forcing it to end with a / on it. This example states that a / can exist, but may not, so you don't need to write a second rule for it.
    Change you're characters with whatever you want to allow (mines just a word character, so a-z0-9 and _)
    Code:
    RewriteRule ^([\w]+)/?$ $1.php
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Greedy am I!

    The word character is a great idea.

    I also made it work like this, (it's a bit more involved, and has a few more features):

    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ help/$1 [R=301,L]
    
    # If the requested URI does not contain a period in the final path-part 
    RewriteCond %{REQUEST_URI} !(\.[^./]+)$ 
    # and if it does not exist as a directory 
    RewriteCond %{REQUEST_FILENAME} !-d 
    # and if it does not exist as a file 
    RewriteCond %{REQUEST_FILENAME} !-f 
    # then add .html to get the actual filename 
    RewriteRule ^([^/]*) help/$1.php [L]
    ## Externally redirect clients directly requesting .php page URIs to extensionless URIs 
    
     
    # If client request header contains php file extension 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP 
    # externally redirect to extensionless URI 
    RewriteRule ^(.+)\.php$ help/$1 [R=301,L]
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •