how to hide .php from address bar
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.ph开发者_运维技巧p
shows like http://www.example.com/index
in the browser's address bar.
How can i do this?
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.
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!!
You'll want to find the appropriate method of url-rewriting for your web server. It lets you map
www.domain.com/page
to
www.domain.com/page.php
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]
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
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/
See Change URL Address make short in PHP
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.
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.
Here is a beginners tutorial for URL rewriting.
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]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
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]
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.
精彩评论