put links without file extension (.php)

前端 未结 5 1708
花落未央
花落未央 2020-12-07 21:29

Is it possible to configure Apache in order not to show a file extension?

For example: Say I have domain.com/page.php but want to have domain.com/

相关标签:
5条回答
  • 2020-12-07 21:34

    There is also MultiViews as a vhost or .htaccess configuration option. See http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews

    From that page:

    The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

    0 讨论(0)
  • 2020-12-07 21:41

    Put this is your .htaccess file

    #turn on url rewriting 
    RewriteEngine on
    
    #remove the need for .php extention 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)$ $1.php
    

    This allows you to access .php files without the extension, so your links should read

    href="/somepage"
    

    and this will direct to

    href="/somepage.php" 
    
    0 讨论(0)
  • 2020-12-07 21:41

    Put this is your .htaccess file

    RewriteEngine On
    RewriteRule ^page?$ page.php

    <a href="page">page</a>

    0 讨论(0)
  • 2020-12-07 21:51

    This is called URL rewriting. I had to use it for the first time recently and i used this tutorial to learn it, hope you'll find it great too :

    0 讨论(0)
  • 2020-12-07 21:55

    You want a web server feature called URL rewriting - every web server application (apache, IIS, nginx) supports this. As the name suggests, it takes the requested URL and rewrites it into a specific format that you define.

    There are many guides available on the www, even if you are using shared hosting solution you can still add/modify the .htaccess file to do this.

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