PHP GET request but no ?= - php

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

Related

Mapping URL into custom files

I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.

How to rewrite profile URL without .htaccess, in PHP?

I am building a router in PHP. I was wondering how I can map /profile?id=3 to /user/3. Is there any way to do this in PHP without .htaccess?
What you could do is create a base entry point which catch all urls like in Symfony, for example.
This entry point could be a app.php file at the root of your project. You can access it via urls like http://yourdomain.com/app.php/user/3.
This entry point will use the URL part after app.php to invoque corresponding controller, extract the user ID and render HTML.
Then, in your apache config, you create a rewrite rule that will prepend all URLs with app.php. http://yourdomain.com/user/3 will be mapped under the hood by apache to http://yourdomain.com/app.php/user/3.
An exemple of such a rewrite rule can be found here.
After that, if you want to support other routes, like /myblog/great-entry or anything else, you can handle them from PHP side. You won't have to edit your apache config ever again, because every URL are catched by your app.php file.
The URL needs to be rewritten from that shortened form, to a query string to be passed to PHP. You cannot accept /user/3 in PHP in any way as it's not going to be populated in the $_GET superglobal array.
You don't need to put rewrite rules in .htaccess files, as they're per-directory inherited. But you still need mod_rewrite.
As you're using PHP, I'm assuming you're not running it on the CLI, so you must be using Apache or Nginx. That being the case, what's the problem running mod_rewrite and putting the rules in the config?

How to manage URLs in 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/

hidequery string parameters from the url using url rewriting

In my php web site, I need to display my urn in a specific manner
I need to hide the query string parameters and just want to display the value in the URL
i.e.
mydomain.com/city.php?place='usa'&id=2
I need to display
my domain.com/city-2/usa
Is it possible?
Please help me. I am in a serious situation.
Thanks in advance
Yes, it is possible:
RewriteEngine On
RewriteBase /
RewriteRule ^city-(\d+)/(.*) city.php?place=$2&id=$1
You need to put it into the .htaccess placed in the web root of your site. To get it worked you need to have .htaccess parsing enabled and mod_rewrite apache module (assuming you have apache as a web server) enabled as well.
More in official documentation

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