Remove .php from URL

后端 未结 11 899
生来不讨喜
生来不讨喜 2020-12-15 22:17

Ubuntu 14.04LTS 32bit

LAMP

I know it\'s an old question but..

I need it to remove .php anywhere it finds it from the visible url. It needs to work wi

相关标签:
11条回答
  • 2020-12-15 22:32

    try this to remove .php extensions completly from your file and to avoid infinite loop:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [NC,L]
    

    This code will work in Root/.htaccess, Be sure to change the RewriteBase if you want to place this to a htaccess file in sub directory.

    0 讨论(0)
  • 2020-12-15 22:33

    Maybe this could be a duplicate, but take a look at this:

    How to remove .html from URL

    In the solution, just change html to php

    0 讨论(0)
  • 2020-12-15 22:34

    Hope helped.

    It's worked for me.

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # For LocalHost !.php
    RewriteCond %{HTTP_HOST} !=localhost
    RewriteCond %{HTTP_HOST} !=127.0.0.1
    RewriteCond %{REMOTE_ADDR} !=127.0.0.1
    RewriteCond %{REMOTE_ADDR} !=::1
    
    ## hide .php extension
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=302,L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    
    0 讨论(0)
  • 2020-12-15 22:45

    Enabling mod_negotiation in my Apache config did the trick for me:

    Content negotiation, or more accurately content selection, is the selection of the document that best matches the clients capabilities, from one of several available documents.

    0 讨论(0)
  • 2020-12-15 22:48

    You may be on the right track. However, it sounds like your .htaccess file is not being executed. Just because a module is activated, does not mean it is available for you in your particular situation.

    Here are some steps to solve your issue:

    • First of all, check the spelling very carefully. Verify that it is spelled correctly (including the . at the beginning)
    • Check the file permissions. Some servers are going to require executable permissions. So chmod the file to 755.
    • If you still do not have it working, go into your apache configuration (probably at /etc/apache2/apache2.conf on Ubuntu) and find every instance of AllowOverride. It might be set to 'none'. Change this to AllowOverride all instead.
    • Then go into sites-enabled, find your site configuration, and change the AllowOverride fields there are well.
    • Restart your Apache server and congratulate yourself with a big cup of coffee.

    One of these should fix it. I would recommend trying between each step so that you can pinpoint where the error occurred. After determining the cause, you may want to go back and restrict some of those AllowOverrides, depending on your needs.

    Best of luck!

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