2

I would like to know if its possible change the extension .php to "/".

Example:

http://localhost/website/example.php?id=19

convert into,

http://localhost/website/example/?id=19

Or if you guys have a better idea for the address i would like to know what should i use.

Im beginner at clean URLS.

Cumps.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Bruno
  • 99
  • 1
  • 10
  • Yes, use the rewrite rule. – frz3993 Jul 15 '15 at 16:25
  • Just for the record, replacing `.php` with a `/` will likely cause many problems. The browser will see it and think it is a folder. Then if you reference another page in the same directory, the browser will look for that page under that directory. So `whatever.com/website/example/` linking to the page `foo` will request the page `whatever.com/website/example/foo/` and not `whatever.com/website/foo/`. Same with all requests for external resources like img and script. You now have to use absolute urls for everything on the site. Not hard, but unless you already do that it will break all over. – Jonathan Kuhn Jul 15 '15 at 16:37

1 Answers1

5

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Is there any need to change in my php pages some code? because it actually removes .php from address but shows that the page doesnt exist. – Bruno Jul 15 '15 at 16:33
  • Are you keeping this `.htaccess` inside `website` folder? If yes then move it a level above that directory. – anubhava Jul 15 '15 at 16:43
  • Oh it worked thanks alot. Btw can i possible convert this one http://localhost/example/search.php?search=keyword&page=4 into http://localhost/example/keyword/4/ if yes would love to know how. Cumps. – Bruno Jul 15 '15 at 16:55
  • @Bruno: Yes it can be done but that makes it a totally different question. Also I have answered those type of questions many times. [Please search here](http://stackoverflow.com/questions/tagged/.htaccess) – anubhava Jul 15 '15 at 17:11