13

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?

2

14 Answers 14

23

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.
6

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!!

Comments

4
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] 

1 Comment

swear I tried 50+ .htaccess solutions, THIS is the only one that worked for me. Even chatgpt failed (many times). THANK YOU!!!!
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

Comments

2

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

Comments

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/

Comments

1

See Change URL Address make short in PHP

Comments

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.

Comments

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.

Comments

1

Here is a beginners tutorial for URL rewriting.

Comments

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] 

Comments

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>

1 Comment

Welcome to StackOverflow! Where exactly would one place the above code? Please be concise on the context of any code posted.
0

you can use this code for hide .php and if isset .php show error

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php # Return 404 if original request is .php RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" RewriteRule .* - [L,R=404] 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.