htaccess code needed- To try .php extension before 404

Discussion in 'Programming' started by floodrod, Mar 27, 2012.

  1. #1
    Hi, I am looking for some htaccess lines, this is what I want to do:

    www.domain.com/page1.shtml it would usually 404 because I only have page1.php. The extensions don't match..

    The obvious answer is 301 redirect page1.shtml to page1.php But the problem is I have over 18,000 pages that need to be done..

    So I would like to add a line in htaccess that says: If no match, try replacing the extension with .php BEFORE 404-ing...

    I already have a 404 script that does this- it will forward people to the new php page- BUT it would be much better for search spiders if it redirected to a .PHP extension BEFORE it 404's/

    Any help?
     
    floodrod, Mar 27, 2012 IP
  2. digiface

    digiface Greenhorn

    Messages:
    44
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    23
  3. floodrod

    floodrod Well-Known Member

    Messages:
    829
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    135
    #3
    NO- No help there. And I know how to use google.

    If you want to give me a link to read- please verify the link addresses the issue I asked about.

    This is what I need:
    If file does not exist- change extension to .php and check for a match. If match=YES 301 to the .php page. If no php page exists either- only then 404

    thanks!
     
    floodrod, Mar 27, 2012 IP
  4. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #4
    Try this:
    
    RewriteEngine on  
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)\.php$ $1.html [nc]
    
    Code (markup):
     
    Arttu, Mar 28, 2012 IP
  5. floodrod

    floodrod Well-Known Member

    Messages:
    829
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Thanks I will try. 1 question first- what's this part- "$$1.html [nc]" ?
     
    floodrod, Mar 28, 2012 IP
  6. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #6
    The "$1" part will replaced whatever is on ^(.*)\ place, so for example something.php becomes something.html and the [nc] means no case sensitive, you can leave it out if you want.
     
    Arttu, Mar 28, 2012 IP
  7. floodrod

    floodrod Well-Known Member

    Messages:
    829
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    135
    #7

    Nono- the html part.. $1.html [nc] why is .html in there?

    I want ALL PAGES that are not found to try the .php extension before 404-ing. I don't want it to try any other extensions. Only php

    Thanks by the way!
     
    floodrod, Mar 28, 2012 IP
  8. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #8
    Sorry about that, this should be what you want:

    
    RewriteEngine on  
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)\.shtml$ $1.php [nc]
    
    Code (markup):
     
    Arttu, Mar 28, 2012 IP
  9. floodrod

    floodrod Well-Known Member

    Messages:
    829
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    135
    #9
    Ok thanks- that code almost works right. It does show the right .php page but the address bar still says .shtml. I was hoping the code would 301 redirect to the .php page and the address bar should show the correct URL.

    Possible?
     
    floodrod, Mar 28, 2012 IP