Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove file extension from a website address?

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 the .php extension to be removed in the 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 Stack Overflow website address itself.

How can I get this done?

like image 424
sumit Avatar asked Jun 30 '11 12:06

sumit


People also ask

How do I remove page file extension from URL?

The . html extension can be easily removed by editing the . htaccess file.


2 Answers

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

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

More about how this works in these pages: mod_rewrite guide (introduction, using it), reference documentation

like image 178
Igor Avatar answered Oct 10 '22 17:10

Igor


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.)

like image 39
Shiv Singh Avatar answered Oct 10 '22 17:10

Shiv Singh