How to manage URLs in PHP - php

I have urls like:
example.com/category?id=5,
but I want this url look like
example.com/category/category_name.
I use pure php without any framework.
Can you please suggest me any way to do it..?

This is something you should try to solve with your webserver, it's not really an PHP question. For Apache, the mechanism is called modRewrite.
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html should be an easy starter.

If under Apache, use some .htacces mechanism.
Anyway your beautified url should contain some reliable data as the category id, not only the category (sluggified) name.
Let's say you want an url like :
example.com/category/category_name/id
In your .htaccess you'll use a rule like :
RewriteRule ^example.com/category/([a-z0-9\-]+)/(\d+)$ /example.com/category?id=$1 [L,R=301]
Check your web sertver documentation for details explanations

look into using mod rewrite as part of apache. You will have a .htaccess file in the root of the web server (if not create 1 and then add the rules you need)
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
there is a site that you can provide a url from a page on your site and it will create the mod rewrite code for you
http://www.generateit.net/mod-rewrite/

Related

PHP GET request but no ?=

I am trying to find out how websites like Imgur, MEGA and such are able to do this:
https://imgur.com/a/SbmNz (emphasis on SbmNz)
The SbmNz bit is dynamic between images or files and I guess it is a kind of $_GET. And I was just wondering how you can do this without the usual ?name=value way.
you can use any MVC or just write a htaccess rule for it , for instance, you can use laravel to pass variables along with url
for laravel refer URL Generation-Laravel5
to write .htaccess refer USING .HTACCESS REWRITE RULES
You can do with Apache rewriting module by changing .htaccess (Create .htaccess file in your folder)
For example if you have this snippet
RewriteRule ^play/([^/]*)$ player.php?id=$1 [L]
When user visits www.example.com/play/Trg4 in backend request is actually handled for www.example.com/player.php?id=Trg4
Then you can get the id with $_GET['id']. it means there is no change with php code. it is completely done with .htaccess
Make sure you enabled rewrite_module of your Apache server

Treat directory as request for a PHP file

This question is related to PHP
How do I make a request to a directory on my server (that doesn't exist) become treated as a variable.. For example:
domain.com/username will really be a request to domain.com/profile.php?user=username
Is this even possible? Or how does YouTube/Twitter/Facebook do it?
Jeff, this is called "friendly URL" and is done with url rewrite. I recommend reading this documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html.
If you are not familiar with regular expressions you shoud read http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
If you using apache as web server create a .htaccess file on your directory with following content to achieve this
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_.-]+$ profile.php?user=$1 [L]
Your looking for mod rewrite. Heres a short tutorial
Most PHP frameworks have libraries that make this much easier, your best bet is probably to use one of them.

Replace articles.php?id=123 with /articles/123-title-of-article

I am building a php+mysql site which will have numerous articles. I am pretty ok with html php jquery etc. I need to now what are the steps I need to take in order not have http://www.mysite.com/articles.php?id=123 but to have http://www.mysite.com/articles/123-title-of-article?
Thanks
Well, you need, for instance, to store the token for each article (to optimize), like your example "123-title-of-article". Next step is to use .htaccess and mod_rewrite from Apache (if is your case).
If you will do it only on this articles "page folder" (articles/), I suggest to you make this folder valid and make the .htaccess inside that and redirect to articles.php?id={POST_ID_FOUND_BY_TOKEN}.
Take a look too on $_SERVER["REQUEST_*"] variables from PHP.
See: this page, on end of article have some examples.
The usual way to do this is by using mod_rewrite.
You create a .htaccess file which, behind the scene, redirects the latter request to the former.
You can read about it here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
You'll need something called mod_rewrite, which is an apache module.
The configuration looks like this (stolen from a client's drupal install):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule articles/(.*)$ articles.php?id=$1 [L,QSA]
</IfModule>
I haven't tested this, but it should work.

How to make a virtual Directory?

http://wiki.answers.com/Q/What_do_the_abbreviations_mean_on_an_ultrasound_scan
In above url, it looks like "What_do_the_abbreviations_mean_on_an_ultrasound_scan" is a directory!! but i think its a virtual directory !!
I want to know about this stuff !! how to do these things and what they call ???
They are most probably using mod_rewrite to rewrite the url. A good resource is the tag wiki for mod_rewrite here on StackOverflow.
The rules for mod_rewrite is stored in the .htaccess file, and looks something like this:
RewriteRule ^/Q/(.*?)$ answers.php?slug=$1
This would rewrite all requests whitch match a url starting with /Q/ to answers.php, and provide whatever is after (in this case What_do_the_abbreviations_mean_on_an_ultrasound_scan) as a GET parameter. This would be accessible in the script as $_GET['slug'] if they were running PHP.

Domain/URL Masking

I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...

Categories