How to remove file extension from website address?

2019-01-01 08:19发布

I am designing a website. I want my website address to look like the following image:

file name extensions like (PHP/JSP) are hidden

I don't want my website to look like http://something.com/profile.php I want .php extension to be removed in address bar when someone opens my website. In other words I want my website to be like: http://something.com/profile

As a second example, you can look at the StackOverflow website address itself.

Can someone please help me in getting this done? Thanks!

10条回答
孤独寂梦人
2楼-- · 2019-01-01 09:15

Here is a simple PHP way that I use.
If a page is requested with the .php extension then a new request is made without the .php extension. The .php extension is then no longer shown in the browser's address field.

I came up with this solution because none of the many .htaccess suggestions worked for me and it was quicker to implement this in PHP than trying to find out why the .htaccess did not work on my server.

Put this at the beginning of each PHP file (preferrably before anything else):

include_once('scripts.php');  
strip_php_extension();  

Then put these functions in the file 'scripts.php':

//==== Strip .php extension from requested URI  
function strip_php_extension()  
{  
  $uri = $_SERVER['REQUEST_URI'];  
  $ext = substr(strrchr($uri, '.'), 1);  
  if ($ext == 'php')  
  {  
    $url = substr($uri, 0, strrpos($uri, '.'));  
    redirect($url);  
  }  
}  

//==== Redirect. Try PHP header redirect, then Java, then http redirect
function redirect($url)  
{  
  if (!headers_sent())  
  {  
    /* If headers not yet sent => do php redirect */  
    header('Location: '.$url);  
    exit;  
  }  
  else  
  {
    /* If headers already sent => do javaScript redirect */  
    echo '<script type="text/javascript">';  
    echo 'window.location.href="'.$url.'";';  
    echo '</script>';  

    /* If javaScript is disabled => do html redirect */  
    echo '<noscript>';  
    echo '<meta http-equiv="refresh" content="0; url='.$url.'" />';  
    echo '</noscript>';  
    exit;  
  }  
}  

Obviously you still need to have setup Apache to redirect any request without extension to the file with the extension. The above solution simply checks if the requested URI has an extension, if it does it requests the URI without the extension. Then Apache does the redirect to the file with the extension, but only the requested URI (without the extension) is shown in the browser's address field. The advantage is that all your "href" links in your code can still have the full filename, i.e. including the .php extension.

查看更多
栀子花@的思念
3楼-- · 2019-01-01 09:16

same as Igor but should work without line 2:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
查看更多
皆成旧梦
4楼-- · 2019-01-01 09:22

Remove a File Extenstion through .htaccess

Original URL: http://ravinderrathore.herobo.com/contact.php

htaccess rule to remove .php, .html etc. file extension from url.

RewriteRule ^(.*)$ $1.php

After Rewriting: http://ravinderrathore.herobo.com/contact

RewriteEngine on

RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/

RewriteRule ^index(.*)?$ index.php$1 [L,QSA]

RewriteRule ^login_success(/)?$ login_success.php [L,QSA]

RewriteRule ^contact(/)?$ contact.php [L,QSA]
查看更多
妖精总统
5楼-- · 2019-01-01 09:23

Actually, the simplest way to manipulate this is to

  1. Open a new folder on your server, e.g. "Data"
  2. Put index.php (or index.html) in it

And then the URL www.yoursite.com/data will read that index.php file. If you want to take it further, open a subfolder (e.g. "List") in it, put another index.php in that folder and you can have www.yoursite.com/data/list run that PHP file.

This way you can have full control over this, very useful for SEO.

查看更多
登录 后发表回答