1

I've seen that WordPress has a feature where you can change the permalink of your pages to different formats e.g. www.example.com/index.html to www.example.com/index.

How do you do that in localhost or in general any host without using WordPress for example if I have the page www.example.com/about.php I want it to be search in a browsers address bar as www.example.com/about

Is there a way to do this?

user3574492
  • 6,225
  • 9
  • 52
  • 105

1 Answers1

0

Use .htaccess

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

The code above will get rid of the .php extension, thus giving you say index instead of index.php. And yes it works on localhost and nearly all hosting companies include it.

Idris
  • 997
  • 2
  • 10
  • 27
  • Where is the `.htaccess` file located in localhost? I can't seem to find it – user3574492 Apr 27 '14 at 14:25
  • Just create a plain text file and name it `.htaccess` and pop it into your root folder of your localhost server – Idris Apr 27 '14 at 14:34
  • what extension? `.txt`? – user3574492 Apr 27 '14 at 14:42
  • No. Just `.htaccess`. Thats what you name the file – Idris Apr 27 '14 at 14:42
  • I've got the `.htaccess` file working but where do I put the file? in the `www` directory? – user3574492 Apr 27 '14 at 14:51
  • If thats the root directory then yes. Where are your files located? – Idris Apr 27 '14 at 16:22
  • That is the root directory and all my files are there, I am getting this error when I try to open any of my files now: `500 Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.` – user3574492 Apr 27 '14 at 16:59
  • Is `rewrite_module` enabled? – Idris Apr 27 '14 at 17:01
  • Oh, that's what it was... Thanks works now... but say if I use an online host for this... do I simply add that to the `.htaccess` file or would I have to mess with other things? – user3574492 Apr 27 '14 at 17:05
  • That's all you simply do with _most_ hosts. Theres probably only 1% of hosts that don't support it. Just pop it in, all you'll be good to go. If this was helpful marking it as answered and upvoting would be much appreciated. – Idris Apr 27 '14 at 17:06
  • Thanks, I don't have enough rep to upvote :p but when I do I sure will. :) – user3574492 Apr 27 '14 at 17:09
  • If I were to do the same for `.html` files then how would I do that? – user3574492 Apr 27 '14 at 19:41
  • Change the last line with `RewriteRule ^([^\.]+)$ $1.html [NC,L]` – Idris Apr 27 '14 at 19:43
  • I want it to happen for both `html` and `php` so do I just add that line? – user3574492 Apr 27 '14 at 20:08
  • Just rename then from `.html` to `.php`. Since HTML can be put into any PHP file. Then just use the line in my answer. I'm not home at the moment, but I find a way to get both of them if you must use HTML – Idris Apr 27 '14 at 20:14