Remove .php extension with .htaccess

前端 未结 15 2160
一生所求
一生所求 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 05:01

    Try

    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME}.php -f 
    RewriteRule ^(.*)$ $1.php [L] 
    
    0 讨论(0)
  • 2020-11-21 05:03

    To remove the .php extension from a PHP file for example yoursite.com/about.php to yoursite.com/about Follow these step . Open .htaccess(create new one if not exists) file from root of your website, and add the following code.

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

    To remove the .html extension from a html file for example yoursite.com/about.html to yoursite.com/about Follow these step .

    Open .htaccess(create new one if not exists) file from root of your website, and add the following code.

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

    Reference: How to Remove php Extention from URL

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

    I found 100% working Concept for me:

    # Options is required by Many Hosting
    Options +MultiViews
    
    RewriteEngine on
    
    # For .php & .html URL's:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    RewriteRule ^([^\.]+)$ $1.html [NC,L]
    

    Use this code in Root of your website .htaccess file like :

    offline - wamp\www\YourWebDir

    online - public_html/

    If it doesn't work correct, then change the settings of your Wamp Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left click rewrite_module

    0 讨论(0)
  • 2020-11-21 05:05

    If you're coding in PHP and want to remove .php so you can have a URL like:

    http://yourdomain.com/blah -> which points to /blah.php

    This is all you need:

    <IfModule mod_rewrite.c>
        RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
    </IfModule>
    
    0 讨论(0)
  • 2020-11-21 05:07

    Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.

    Re 1) Change the .html to .php

    Re a.) Yup, that's possible, just add #tab to the URL.

    Re b.) That's possible using QSA (Query String Append), see below.

    This should also work in a sub-directory path:

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
    
    0 讨论(0)
  • 2020-11-21 05:07

    I've ended up with the following working code:

    RewriteEngine on 
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [NC,L,R]
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [NC,L]
    
    0 讨论(0)
提交回复
热议问题