-2

I want to know how can I set the URL of a website in the form: http://example.com/category/1/ instead of http://example.com/category.php?value=1

I have heard that the first style of URL is better in SEO and is more safe.

Further for second URL Style w set the url from first page as:http://example.com/category?value=$i where $i can be any value and on the category.php page we get the value using the $value=$_GET['value'];

How can we do the same using the first URL STYLE?

Retsam
  • 30,909
  • 11
  • 68
  • 90
  • If you want to use like the 1st url, you must to use API Rest – bicho Aug 20 '15 at 21:23
  • 2
    possible duplicate of [Pretty URLs for search pages](http://stackoverflow.com/questions/128796/pretty-urls-for-search-pages) – chris85 Aug 20 '15 at 21:24
  • possible duplicate of [.htaccess rewrite query string as path](http://stackoverflow.com/questions/13003319/htaccess-rewrite-query-string-as-path) – Tim Aug 20 '15 at 21:25

1 Answers1

1

This isn't something that can be done with PHP. Instead, you should use your .htaccess file to rewrite query strings as paths.

From this answer on SO,

In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]

And in order to redirect the query string URL to the nicer looking one:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+)
RewriteRule ^/?test\.php$ /test/%1? [L,R=301]
Community
  • 1
  • 1
Tim
  • 2,123
  • 4
  • 27
  • 44
  • 3
    Yeap, this is pretty much the answer the Op should be looking for. The only additional info I would give is: The above rules are for Apache. If you use nginx or any other webserver, you should have a look at the syntax. The RewriteRule needs, in this case to be written for each file you which to serve. It would be easier to rewrite everything to index.php, and from there analize which file should be 'included' (consider using a framework or checking php 'traditional' design patterns) – Bolovsky Aug 20 '15 at 21:29