10

I use php to build web applications, but i want my web pages without .php extension in the browser's address bar. For eample http://www.example.com/index.php shows like http://www.example.com/index in the browser's address bar.

How can i do this?

Linga
  • 9,691
  • 9
  • 45
  • 91
World
  • 1,849
  • 7
  • 25
  • 37
  • you have to use htaccess for that – Atul Dravid Apr 29 '11 at 11:58
  • Some kind of related: [How come some site urls do not include a file extension?](http://stackoverflow.com/questions/3631153/how-come-some-site-urls-do-not-include-a-file-extension) – Gumbo Apr 29 '11 at 12:04

13 Answers13

15

Put this in a file named .htaccess in your WWW-root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php

This works if you're running Apache and have mod_rewrite activated.

Karl Laurentius Roos
  • 4,110
  • 1
  • 31
  • 38
  • 1
    @ling.s: saw your comment now. Of course. The important thing here is the RewriteRule, which matches the full path of any request URI to any PHP file and passes that to the file $1.php, $1 being the first (and only) match from that regular expression. – Karl Laurentius Roos Nov 26 '15 at 10:12
5

Just create a .htaccess file in wamp/www/ and copy-paste this code..

Options +FollowSymlinks


RewriteEngine On


RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f


RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.+)$ /$1.php [L,QSA]

Hope! this would be useful for someone!!

Purushottam
  • 784
  • 12
  • 23
3
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
3

You'll want to find the appropriate method of for your web server. It lets you map

www.domain.com/page

to

www.domain.com/page.php

Dutchie432
  • 27,738
  • 20
  • 88
  • 109
1
little modification - better to use REQUEST_URI and check for the directory


# Turn mod_rewrite on

RewriteEngine on

# To externally redirect /dir/foo.html to /dir/foo/
RewriteCond %{REQUEST_URI} ^(.+)\.html?$ [NC]
RewriteRule ^ %1 [R,L]

# To internally redirect /dir/foo/ to /dir/foo.html
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]

# To externally redirect /dir/foo.html to /dir/foo/
RewriteCond %{REQUEST_URI} ^(.+)\.html?$ [NC]
RewriteRule ^ %1 [R,L]

# To internally redirect /dir/foo/ to /dir/foo.html
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
hcktheroot
  • 11
  • 1
1

On systems using the Apache webserver, you would use mod_rewrite.

On newer versions of IIS, you can use their version of mod_rewrite. On older versions you need to buy a plugin.

Stack Overflow article

Search Stack Overflow and you should find answers to questions already asked.

Community
  • 1
  • 1
SeraM
  • 486
  • 4
  • 10
1

Just to point out that on older versions of IIS for example IIS6 and assuming you are in a 32 bit process then IONICS ISAPI Rewrite is a fantastic free url rewriting module. Inside of 64 bit in IIS 6 I have found the commercial product Helicon ISAPI Rewrite 3 to be a great tool. But if you are in 32 bit, IONICS is free and does everything you will require.

http://iirf.codeplex.com/

REA_ANDREW
  • 10,132
  • 8
  • 45
  • 70
1

See Change URL Address make short in PHP

Community
  • 1
  • 1
kempsam
  • 56
  • 2
1

In apache2.conf I have

<Files data>    
 ForceType application/x-httpd-php    
</Files>

Which means data is treated as a PHP file without the extension

pavium
  • 13,768
  • 4
  • 29
  • 45
1

There are several ways of doing it.

You can use mod-rewrite to rewire foo to foo.php so that requests for /bar gets handled by /bar.php.

You can use directories, and default-files, so that you link to the direcory /foo/ which gets handled by /foo/index.php

You can set a php-script as the handler for 404-errors, then you just link to nonexistant files, and the handler-file deals with it however it likes. (typically by using some sort of map from url to php-file)

You can tell your webserver that all request for a certain webserver, is to be handled by php.

The first or second solution is the simplest, but the 2 last ones gives the best flexibility, and variants thereof is what most of the bigger frameworks do.

Agrajag
  • 1,018
  • 2
  • 9
  • 23
1

Here is a beginners tutorial for URL rewriting.

NikiC
  • 95,987
  • 31
  • 182
  • 219
mcgrailm
  • 16,637
  • 21
  • 80
  • 128
0

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
  • Welcome to StackOverflow! Where exactly would one place the above code? Please be concise on the context of any code posted. – 1000Nettles Mar 03 '18 at 03:29
-1

Insert this code into your .htaccess file on the remote server:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

This will rewrite the URLs in the way you intended.

serraosays
  • 5,487
  • 1
  • 28
  • 49