I have my htaccess redirecting to a error404.php but I want that PHP file to be able to extract the original url parameters.
For example:
http://mywebsite.com/unknownfile.php?param1=value1¶m2=value2
I've tried $_GET['param1'] but that's empty. The $_SERVER['PHP_SELF'] just shows error404.php
Many thanks in advance.
You can access original URL via $_SERVER["REQUEST_URI"].
For example: requesting URL /hihi/meow?key=rumba which does not exists. The $_SERVER["REQUEST_URI"] will have that string, which you can parse with parse_url() function to split into parts and use other functions (like explode() to get to individual query strung parameters.
If you redirect this info will only be present in $_SERVER['HTTP_REFERER']
Related
I want to get the correct URL with PHP without any error, my links example:
https://example.com/
https://example.com/search/
https://example.com/search/?q=test
https://it.example.com/
https://it.example.com/search/
https://it.example.com/search/?q=test
so i want to get all link if is https://example.com/ show example.com if is https://example.com/search/ show example.com/search/ if is https://it.example.com/search/?q=test show example.com/search/?q=test etc.. without any error. thanks
Looks like you need $_SERVER['REQUEST_URI']:
$link = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Have a look over the PHP documents too: http://php.net/manual/en/reserved.variables.server.php
This will return the HTTP Host without https, and will also get you the request_uri with query strings etc.
parse_url() will also give you each element, and then you can build up the string you need:
http://php.net/manual/en/function.parse-url.php
i need help with my code:
Im including pages in index.php like:
if(isset($_GET['xx'])
{
// include('yy/'.$_GET['xx'].'.php');
}
and now in the file page.php i wanna use get parameters like from and to.
i use htaccess for short url
RewriteRule ^([a-z]*)$ ./main.php?xx=$1
So i get this url in final:
index.php?xx=page?from=a&to=b
When i print_r($_GET) i got only first parameter xx
$_POST parameter works fine, but i need it with $_GET.
Look at the URL:
index.php?xx=page?from=a&to=b
It should be
index.php?xx=page&from=a&to=b
You put ? in there twice instead of a &
Solved!
by misorude
By specifying your own query string in the replacement, you are
discarding the original one - this is default behavior with
mod_rewrite. You need to specify the QSA flag to keep it.
httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
Thanks for fast responds without reading. Have a nice day!
I am trying to get the value after the / in a URL in PHP.
I have tried using $_GET['va'], but this only works for the following,
http://localhost:8080/default?va=xxx
What I'm trying to get is this,
http://localhost:8080/default/xxx
How do I get the xxx value after a / in PHP.
Any help is greatly appreciated.
Edit
Thanks to everyone who answered, I wasn't very clear in stating what I wanted. It appears what I was looking for is known as a pretty URL or a clean URL.
I solved this by following Pedro Amaral Couto's answer.
Here is what I did;
I modified my .htaccess file on my Apache server, and added the following code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ default.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ default.php?page=$1
Then I modified my default.php file to GET ['page']
<?php
echo $_GET['page'];
?>
And it returned the value after the "/".
You want to make what is called "pretty URLs" (and other names).
You need to configure the HTTP server appropriately, otherwise you'll get a 404 error. If you're using Apache, that means you may configure .htaccess with RewriteEngine module activated. You also need to add regular expressions.
There's already a question in StackOverflow concerning that subject:
Pretty URLs with .htaccess
Here are another relevant articles that may help:
http://www.desiquintans.com/cleanurls
https://medium.com/#ArthurFinkler/friendly-urls-for-static-php-files-using-htaccess-3264e7622373
You can see how it's done in Wordpress:
https://codex.wordpress.org/Using_Permalinks#Where.27s_my_.htaccess_file.3F
If you follow those, you won't need to change the PHP code, you may use $_GET to retrieve "xxx".
You are looking for: $_SERVER['REQUEST_URI']
The URI which was given in order to access this page; for instance, '/index.html'.
basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
So the $_GET global variable is for parsing the query string.
What you're looking for is the $_SERVER['REQUEST_URI'] global variable:
$url = $_SERVER['REQUEST_URI'];
$url will now contain the full URL of your path. You'll need to use explode('/', $url) to break up that full URL into an array of little strings and parse it from there.
Example:
$pieces = explode('/', $url);
// this will get you the first string value after / in your URL
echo $pieces[0];
You can do in 2 ways.
1- do these steps
Get URL
Explode by /
Get Last element of array
2- Add .htaccess and map that value for some variable
RewriteRule ^default/(.*) page.php?variable=$1
and you can get $_GET['variable']
Is there any way that I could I could have a parameter in the URL but not using question mark in PHP?
The inputted url as:
http://www.example.com/y/foo
But it actually parses as:
http://www.example.com/y/?y=foo
but the actual URL which the client has is the original URL.
The /y/ directory does exist and so does the index.php file inside of that.. But the /foo file does not exist.
This can be achieved via url rewriting. The url structure you are looking for will not be the actual url but it can replace the actual url and make your url look pretty, while the original url is responsible for the page being displayed.
The foo here is a parameter or supposedly argument for a function present on that page.
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I am using my code like this to generate a canonical link for SEO purposes
<link rel="http://www.mydomain.com$_SERVER[SCRIPT_NAME]">
So lets say the file I go to is http://www.mydomain.com/thisfolder/?this=that&yes=no
The canonical link will display as
http://www.mydomain.com/thisfolder/?this=that&yes=no
What I want is no matter what the extra variables being passed in the URL are that it will display the canonical as
http://www.mydomain.com/thisfolder/
I have tried both REQUEST_URI and SCRIPT_NAME in my $_SERVER[]; but both do the same thing. Is there a way I can achieve this whether I am just not using the correct name to $_SERVER[]; or is there anyway to do this?
Try $_SERVER['PATH_INFO']
Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.
http://php.net/manual/en/reserved.variables.server.php