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 08:59

Tony, your script is ok, but if you have 100 files? Need add this code in all these :

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

I think you include a menu in each php file (probably your menu is showed in all your web pages), so you can add these 2 lines of code only in your menu file. This work for me :D

查看更多
后来的你喜欢了谁
3楼-- · 2019-01-01 09:00

The problem with creating a directory and keeping index.php in it is that

  1. your links with menu will stop functioning
  2. There will be way too many directories. For eg, there will be a seperate directory for each and every question here on stackoverflow

The solutions are 1. MOD REWRITE (as suggested above) 2. use a php code to dynamically include other files in index file. Read a bit more abt it here http://inobscuro.com/tutorials/read/16/

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

You have different choices. One on them is creating a folder named "profile" and rename your "profile.php" to "default.php" and put it into "profile" folder. and you can give orders to this page in this way:

Old page: http://something.com/profile.php?id=a&abc=1

New page: http://something.com/profile/?id=a&abc=1

If you are not satisfied leave a comment for complicated methods.

查看更多
其实,你不懂
5楼-- · 2019-01-01 09:13

Just add .htaccess file to the root folder of your site(for example, /home/domains/domain.com/htdocs/) with following content:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
查看更多
何处买醉
6楼-- · 2019-01-01 09:14

First, verify that the mod_rewrite module is installed. Then, be careful to understand how it works, many people get it backwards.

You don't hide urls or extensions. What you do is create a NEW url that directs to the old one, for example

The URL to put on your web site will be yoursite.com/play?m=asdf

or better yet

yoursite.com/asdf

Even though the directory asdf doesn't exist. Then with mod_rewrite installed you put this in .htaccess. Basically it says, if the requested URL is NOT a file and is NOT a directory, direct it to my script:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /play.php [L] 

Almost done - now you just have to write some stuff into your PHP script to parse out the new URL. You want to do this so that the OLD ones work too - what you do is maintain a system by which the variable is always exactly the same OR create a database table that correlates the "SEO friendly URL" with the product id. An example might be

/Some-Cool-Video (which equals product ID asdf)

The advantage to this? Search engines will index the keywords "Some Cool Video." asdf? Who's going to search for that?

I can't give you specifics of how to program this, but take the query string, strip off the end

yoursite.com/Some-Cool-Video 

turns into "asdf"

Then set the m variable to this

m=asdf

So both URL's will still go to the same product

yoursite.com/play.php?m=asdf 
yoursite.com/Some-Cool-Video 

mod_rewrite can do lots of other important stuff too, Google for it and get it activated on your server (it's probably already installed.)

Reference : http://www.scriptscoop2.com/t/1a0c2b81484d/php-how-to-remove-file-extension-from-website-address.html

查看更多
梦醉为红颜
7楼-- · 2019-01-01 09:14

just nearly the same with the first answer about, but some more advantage.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Just add up if you have a other file-extension in your sites

查看更多
登录 后发表回答