Removing .php file extension with .htaccess file

后端 未结 5 1341
误落风尘
误落风尘 2020-12-10 16:28

I want www.example.com/about.php to just be www.example.com/about

I created an .htaccess file and placed it in the root of my server. I am using linux shared hosting

相关标签:
5条回答
  • 2020-12-10 16:39

    Pasted above everything in my .htaccess file worked for me...

    ## 301 Redirects
    RewriteCond %{QUERY_STRING}  ^$
    RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]
    
    0 讨论(0)
  • 2020-12-10 16:42

    Use this code for hiding/removing .php extension:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## hide .php extension
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L,NC]
    
    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]
    
    0 讨论(0)
  • 2020-12-10 16:46

    Your rewrite rule isn't correct, it's the wrong way round. Try this instead:

    RewriteRule ^(.*).php$ /$1 [L,R=301]
    
    0 讨论(0)
  • 2020-12-10 16:49

    I had this problem also, but I found that this seemed to fix the GoDaddy .htaccess problem.

    # Fix Rewrite
    Options -Multiviews
    
    # Mod Rewrite
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase / 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)$ $1.php 
    
    0 讨论(0)
  • 2020-12-10 17:00

    So after a long bout with google I finally figured this one out. This works with Godaddy shared hosting. It removes php file extensions so http://yoursite.com/about.php becomes http://yoursite.com/about

    Options -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f    #If requested is not a filename...
    RewriteCond %{REQUEST_FILENAME} !-d    #And not a directory
    RewriteRule ^(.*)$ $1.php [L]          # perform this redirect
    

    (remove comments before uploading to server)

    0 讨论(0)
提交回复
热议问题