removing .php from urls ...htaccess?

Discussion in 'HTML & Website Design' started by artboy, Aug 13, 2007.

  1. #1
    I've got alot of pages that will be going live on a site soon, and all have .php extentions.

    I heard it's possible to permanently redirect(301) away from the .php links... anyone know how to do this, or where to get some info?

    basically I would like the urls shortened from: www.site.com/page.php to www.site.com/page (with no .html, .php, or any extension at the end)


    thanks guys...
     
    artboy, Aug 13, 2007 IP
  2. ANick

    ANick Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think it is better to use mod_rewrite for this problem:

    RewriteEngine on
    RewriteRule ^/page /page.php [PT]

    or even more simple:
    RewriteEngine on
    RewriteRule ^/(.+) /$1.php [PT]
    but it will work only with urls such www.site.com/page

    To redirect user to a proper page from .php you can add this code in your php files:
    <?php
    $uri = str_replace(".php", "", getenv("REQUEST_URI"));
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $uri");
    ?>
     
    ANick, Aug 13, 2007 IP
  3. artboy

    artboy Peon

    Messages:
    149
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    All my .php files aren't online yet, so there's no PR or links yet... will the mod-rewrite from .php to no-.php be treated as a 301(perm) redirect for all future linkbuilding?
     
    artboy, Aug 13, 2007 IP
  4. ANick

    ANick Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no, it helps only to associate "/page" and "/page.php". If there are no links to "page.php", everything is ok. 301 is using for moved pages only and your should use only links without ".php" and google and etc will not know about this pages. Your can add my code to prevent using ".php".
     
    ANick, Aug 13, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    ANick, that will also append .php to CSS, JS, images and any other files!

    This works, and also stops duplicate content penalties by disabling access by 301 redirecting .php -> non .php

    RewriteEngine on
    
    # 301 redirect .php -> non .php
    RewriteCond %{REQUEST_URI} ^(.+)\.php$ [NC]
    RewriteRule . %1 [R=301,L]
    
    # Rewrite non .php -> .php ("transparent")
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ $1.php [L]
    
    Code (markup):
     
    krt, Aug 14, 2007 IP