Remove .php extension with .htaccess

前端 未结 15 2218
一生所求
一生所求 2020-11-21 04:32

Yes, I\'ve read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I\'ve come is having it remove the extension, but it point

相关标签:
15条回答
  • 2020-11-21 04:52

    The following code works fine for me:

    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)$ $1.php
    
    0 讨论(0)
  • 2020-11-21 04:55

    After changing the parameter AllowOverride from None to All in /etc/apache2/apache2.conf (Debian 8), following this, the .htaccess file just must contain:

    Options +MultiViews
    AddHandler php5-script php
    AddType text/html php
    

    And it was enough to hide .php extension from files

    0 讨论(0)
  • 2020-11-21 04:55

    Here is the code that I used to hide the .php extension from the filename:

    ## hide .php extension
    # To redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=301,L,NC]
    

    Note: R=301 is for permanent redirect and is recommended to use for SEO purpose. However if one wants just a temporary redirect replace it with just R

    0 讨论(0)
  • 2020-11-21 04:56

    Here's a method if you want to do it for just one specific file:

    RewriteRule ^about$ about.php [L]
    

    Ref: http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/

    0 讨论(0)
  • 2020-11-21 04:57

    If your url in PHP like http://yourdomain.com/demo.php than comes like http://yourdomain.com/demo

    This is all you need:

    create file .htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^([^\.]+)$ $1.html [NC,L]
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    0 讨论(0)
  • 2020-11-21 04:58

    Not sure why the other answers didn't work for me but this code I found did:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    That is all that is in my htaccess and example.com/page shows example.com/page.php

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