Quick mod_rewrite question

Discussion in 'Apache' started by Juanzo, Jan 17, 2008.

  1. #1
    I'd like to extract the .php extension from our URL's and so far, I've been using:
    RewriteEngine On 
    RewriteRule ^new-url$ new-url.php [L]
    RewriteRule ^new-url2$ new-url2.php [L]
    Code (markup):
    I've tried using a wildcard before but it messed up with some files I did wanted to keep the extension. Now I thought I could use a wildcard for every .php5 file, and then rename the files I'd like to have the extension extracted. Would that work? If so, what should I put inside my .htaccess?
     
    Juanzo, Jan 17, 2008 IP
  2. baumann93

    baumann93 Peon

    Messages:
    2,268
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am not an expert, but this might work for you:

    
    RewriteEngine On 
    RewriteRule ^(.*)$ /$1.php [NC,QSA,L]
    
    Code (markup):
    Just make sure the name of the directory is the same as the name of your php file.
     
    baumann93, Jan 22, 2008 IP
  3. Juanzo

    Juanzo Active Member

    Messages:
    139
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Thanks for your answer baumann! So if I use the following code:
    
    RewriteEngine On 
    RewriteRule ^(.*)$ /$1.php5 [NC,QSA,L]
    
    Code (markup):
    I would extract the extension from every URL ending with .php5? (eg. www.domain.com/file.php5 to www.domain.com/file)
     
    Juanzo, Jan 22, 2008 IP
  4. baumann93

    baumann93 Peon

    Messages:
    2,268
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When you write your urls, you would use something like:

    
    http://domain.com/my-super-directory/
    
    Code (markup):
    notice the trailing forward slash. the mod_url will rewrite it to:

    
    http://domain.com/my-super-directory.php
    
    Code (markup):
    there is a small modification to the code I gave you:

    RewriteEngine On 
    RewriteRule ^(.*)/$ /$1.php [NC,QSA,L]
    Code (markup):
    notice the forward slash just before the first $ sign.
     
    baumann93, Jan 22, 2008 IP
  5. Juanzo

    Juanzo Active Member

    Messages:
    139
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Thanks for your answer baumann, but I would like to extract the extension from every URL ending with .php5 and make a 301 redirect if people access it with .php (eg. www.domain.com/file.php5 to www.domain.com/file).

    What code should I use to accomplish that? Does anyone else know?
     
    Juanzo, Jan 23, 2008 IP
  6. Juanzo

    Juanzo Active Member

    Messages:
    139
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    88
    #6
    Any help with this will be greatly appreciated.
     
    Juanzo, Feb 5, 2008 IP