This question already has an answer here:
Friendly URLs in PHP
(1 answer)
Closed 1 year ago.
I am used to SEND all sorts of requests to 'foreign' URLs or to my Website Server URL and receive responses and such.
And thus, i am used to receive requests as well.
BUT
Only if it targets a PHP file like so : http://www.example.com/file.php
In order for a core part of my future website feature to work, i need to be able to listen to a GET request made to a "blank" url like so : http://www.example.com/blank/?key1=value1&key2=value2
Is there a way to 'run' a PHP script whenever this URL is called ? Even if it is not targeted directly in the request ? Any other script ?
I'm kinda ignorant when it comes to server-side mecanisms.
EDIT :
I have tried to create a file.PHP with the following :
$key1 = $_GET['key1'];
$key2 = $_GET['key2'];
echo "$key1 and $key2";
but response is 403 (forbidden)
EDIT2 : To my moderator : the question linked is not the exact same question as mine. He asks to hide the file extension, i ask how to target a directory. Even if the answer has the same source (htaccess) it is not the same thought process.
Further more, my title explicitely mentions requests (hence the tags API and ESI) and not only the php subject.
In general: Yes but how to do it exactly depends on your web server software. For Apache for example, you can use a .htaccess file to create rewrite rules to make paths matching a certain pattern (e.g. /blank/<whatever>) behave as if they were another path (e.g. /blank.php).
But in your case where you actually have /blank/ as "directory", there is an even simpler way: Put an index.php file into the directory.
It's an almost universal convention that if a directory is specified in the URL (without specific filename), the server will look for an index.php file (or index.html, but your question was about PHP in particular) in the directory and direct the request there.
Related
I was looking at a few web e-commerce website and noticed that they usually have long URL something like this (taken from an e-commerce site)
https://www.example.com/bsquare-tuxedo-solid-men-s-suit/p/itmejr4zbjt6fr3h?gclid=CjwKEAiAz4XFBRCW87vj6-28uFMSJAAHeGZbXBpy4Rw5UckmBAiPaacHLhr5MbPj4bMxVThaQe5A3xoCIi7w_wcB&pid=SUIEJR4ZAK7FZKRT&cmpid=content_suit_8965229628_gmc_pla&tgi=sem%2C1%2CG%2C11214002%2Cg%2Csearch%2C%2C95089233620%2C1o5%2C%2C%2Cc%2C%2C%2C%2C%2C%2C%2C&s_kwcid=AL!739!3!95089233620!!!g!182171694500!&ef_id=WJitiQAAAfz4lw1Y%3A20170213091758%3As
Generally a router would be
http://www.example.com/controller/method/param1/param2
Then what all codes go up in the longer URL as above and why do a longer version when a short URL can get the same job done?
You can start to read more about get method:
http://www.html-form-guide.com/php-form/php-form-get.html
To send data server side in php you will need to make a request get or post. The second link is an get but rewritten.In the end to access your data the url will be rewritten by the framework you use ( http://www.example.com/controller/method?id1=param1&id2=param2 ). If you do not use any framework you will need to rewrite yourself with .htaccess Usually the second link is used for
more common pages so will be better indexed by google. The first link is for data that is more dynamic and may have more or less parameters, null or "" values etc. And also you do not care to index that page. For exempla a search : http://www.example.com/controller/method?searchValue=&page=1
You also can read more here. It explain how and why we rewrite links: https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/
I am having a problem with a PHP script, so the link is like this: page.php?id=1412.
But I don't want to get the data using $_GET['id']; is there a way for the program to recognize the URL like this localhost/1412, and then try to find the 1412 in the id field in database?
If possible how can I avoid the Not Found page ? since the folder 1412 does not exist ?
For all of this kind of matching you need to use either .htaccess files for apache, or similar functionality implemented in the configuration files of nginx/lighthttpd and so on. Basicly, when a request like x/1412 will come, you can match it before being dispatched to the php engine and transform it internally into x?id=1412 . Other solution involves redirecting all requests to a front controller (a script that is serving as a single entry point into the application) and let a url matcher match the route to execute. The latter is implemented in about every big/small php framework.
You could regex the current uri ($_SERVER['REQUEST_URI']). See for creating regex: PHP URL Regex and Parameter
.htaccess file is a solution, you can access this type of url and can send the value to php files using .htaccess file refer this nice article for doing this
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mod rewrite and query strings
The only way I know to pass parameters in the URL is using the GET method in PHP. But I saw many websites using what seems to be parameters directly in the URL, like this:
http://.../page/2/
In this case, is "page" really a parameter? If so, how is it handled in the code?
Or is this a regular URL of a directory "2" located in a directory "page"? Would it mean that whenever a new post is created, the website creates all the pages and the corresponding directories?
Thanks
This is called url rewriting. Basically this means that you use an apache module to rewrite incoming urls to new urls which are then handled by apache
In your example http://www.test.com/page/2/ is probably rewritten to something like http://www.test.com/?page=2.
If you search the internet for Apache URL rewrite you will get enough results explaining how you can do this.
These are all conventions. GET parameters are not specific to PHP, this is how all browsers encode form data. Java-based webapps use semicolon-separated parameters for example.
You can write a simple layer to convert ?alpha=1&beta=2 to /alpha/1/beta/2 but iw tould be just a cheap gimmick (except in a very few legitimate cases like with Squid caches).
What today's websites do is to use a single entry point pattern. Usually with apache's mod_rewrite, all requests are handled by the index.php and there is a routing facility to choose the adequate handler PHP file for a specific URL scheme. These schemes can be easily defined by Regular Expressions.
So all in all you decide how your URLs are going to look like. It is not an easy task and there are many SEO snake oil salesmen out there who will make you do all kinds of crazy stuff. What a good URL does is to identify a content (document) inside your service and you must use that single URL throughout your service to address it.
And don't forget: cool URLs don't change. You will abandon your current code base in 2 years and rebuild your site from the ground up. Design your URL scheme in a way that makes sense from a logical point of view and not something dependent on your webapp design.
The example you gave is still a GET request.
What you are looking for is URL rewriting.
I don't know if this is the suitable place to ask this question, so I am sorry if I am doing it wrong. I think this is not a duplicate question. If it is, I am sorry too.
Currently, I have a web app which takes its content from a unique XML document. The URL is "http://webapp.com/"
The problem is that now I have to create a second version of the web which uses a new different XML document, so I have to put them different URLs, something like the following:
http://webapp.com/activities
http://webapp.com/stages
How can I have all my sources on just one directory on server-side (I suppose it's better to maintain, the resources are cached better, etc...) but with these 2 URLs pointing to the folder and making the PHP loades the XML depending on the URL?
I suppose I have to change the .htaccess file to redirect to that folder, but how can I tell what XML I must load? And the user must see on the search bar the URL he used, for example "http://webapp.com/activities"
Can anybody help me please?
Thanks for all
You can use a .htaccess rule to redirect a URL like http://webapp.com/{anything} to http://webapp.com/?q={anything}. Then in your script you can write conditions based on $_GET['q'] and load different XML. That is if I understand your question right.
Hi I'm trying to build the server side of an application using REST archtecture with PHP. later I will make a client for android to consume those services.
I already know what is a RESTful application.
but still got something confuse:
if I want to get retrieve information of a user, the url I should access is www.domain.com/user/123 according to REST. but that means for every user in DB I should make a .php?? that sounds illogical. or there are something like java which use logical url, or just rewrite the url to something like www.domain.com/getusr.php?id=123 ??
fixed: is url rewrite the correct way to interpret request url from clients for a webservice in php?
for example: if someone request www.domain.com/user/123, I should rewrite to www.domain.com/getusr.php?id=123
or maybe php is not for webservice?
thanks
You need to rewrite the url to www.domain.com/getusr.php?id=123 , that way you need only 1 php file , all requests in form of www.domain.com/user/123 would go to a single getusr.php file . You can achive this using apache mod_rewrite ( there are tons of examples around so i whont post another one ) .
Edit
Check this link , that contains a few good examples .
Or you can use this code bellow ( place it inside a ".htaccess" file at the root of you're web directory , where getusr.php should be placed allso )
RewriteEngine on
RewriteRule ^user/([0-9]+)$ getusr.php?id=$1
I like the way Drupal does it. Drupal uses a q parameter for all of its routes:
http://myserver/drupal/?q=node/234
Which, if mod_rewrite is enabled, becomes
http://myserver/drupal/node/234
This way, you just have to parse the contents of $_GET['q'].
It's cleaner than having a bunch of separate rewrite rules for every one of your routes.