-1

I am developing a site in core php and i have link such as

<a href="news_details.php?news_id=$data['news_id']?>">Read More</a> 

So my question is to remove the .php extension from all the site and also from the links and url must be nice like http://www.example.com/news/ not like http://www.example.com/news.php?news_id = 4

So please if any one has idea how to do that please tell me

kero
  • 10,647
  • 5
  • 41
  • 51
  • possible duplicate of [How to do URL re-writing in PHP?](http://stackoverflow.com/questions/1039725/how-to-do-url-re-writing-in-php) Or use regular mod_rewrite instead – kero Feb 11 '15 at 11:20
  • 1
    If you are using a webserver which supports rewriting mods, thats the spot. For example a apache2 module that enables you to do so is called "mod_rewrite". You can write expressions and rules which are applied to files which fall into these rules's scope. Btw: The technique you are looking for is called "smart-url" – serjoscha Feb 11 '15 at 11:21
  • can you please brief me something more about that or have some demo like that thanks – user3565243 Feb 11 '15 at 11:23
  • to be not concrete but a litte more abstract: you can write a rule telling that example.com/news/ is internally (without the user viewing the page can see) always rewritten to example.com/news/news.php?id=. You are lucky. The rule for that should be very easy to write but I would also need to google how it works exactly because I forgot about the exact syntax. Perhaps you just google your self for examples to rewrite URL Parameters and QueryStrings (these things behind ? at the end of the URL) – serjoscha Feb 11 '15 at 11:29
  • thanks for your appreciable support thank you very much serjoscha – user3565243 Feb 11 '15 at 11:33

1 Answers1

0

I will be summarizing my comments so you can mark this post here as solution for the next person having your problem. This makes finding the answer easier somehow

You need a webserver which supports rewriting of URLs. Most people perhaps use the apache2 webserver which does supports this. The module for apache doing this is called "mod_rewrite". You probably (depending on your configuration) need to enable it first.

Clean and beautiful URLs are called "smart-urls", for this term makes searching for tutorials, guides and answers much easier.

To make the mod_rewrite work and rewrite your URLs you need to enable the module for your current project/directory and write some rules down.

You can do this within your .htaccess file and for your example it will look something like this:

.htaccess:

RewriteEngine On
RewriteRule ^news/([0-9]+)\.?.*$ news.php?id=$1 [NC,L]

This will cause the server internally (and without the user can see) to rewrite something like this:

http://www.foo.de/news/1337.my-awesome-first-newspost

to

 http://www.foo.de/news.php?id=1337

Remark: Everything behind the News ID is ignored

As you can see rules are written as regular expressions which enables you to create really flexible rules.

You can also write multiple rules which depend on each other or just become applied one after the other.

I found a page enabling you to test your rules because this is always a bit hard "debugging" when doing it on your server machine:

http://htaccess.madewithlove.be/

serjoscha
  • 510
  • 4
  • 10