Getting Parameters in a URL without question mark - php

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/

Related

How to change change URL format to POST using htacess php?

I have htaccess tha handle redirections know i want to hit api like i have a url like that
http://localhost:8080/test/network/country/category/subcategory/fixedvalue
I have fixed value like detail, category ,codes but only in case of detail i need id as parameter
example
http://localhost:8080/test/Airtel/UK/Prepaid/5G/Detail
below is redirect url that i want to generate from htacess file
if fixed value is detail
http://localhost:8080/h1.php?network=Airtel&country=UK&category=Prepaid&subcategory=5G&Detail=remain portion of url except .html
in other cases where detail is not in fixed i don't need id that have parameter the remaining portion of url after 5th parameter so url will be
http://localhost:8080/h1.php?network=Airtel&country=UK&category=Prepaid&subcategory=5G
here is a code that redirect to 5.php file so that but i want not redirection i want a specific url having parameter
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ 5.php?1=$1&2=$2&3=$3&4=$4&5=$5 [QSA,L]
in simple from
www.cloth.com/nike to www.cloth.com?name=nike

PHP: How to get content of another URL (page) on my site without changing address in bar

I am using ModRewrite as below to convert urls on my site to be SEO friendly:
RewriteRule user/(.*)/$ seo-url-user-by-name.php?username=$1
Now I am writing code for seo-url-user-by-name.php and am looking for a way in PHP to redirect to:
user.php?uid=<uid>
so that seo-url-user-by-name.php will essentially return the contents of user.php?uid=<uid> BUT without changing the address in address bar to user.php?uid=<uid>
How do I do that?
Simply include user.php in seo-url-user-by-name.php. To get the querystring right you have to overwrite the value in $_GET.
$_GET['uid'] = 'whatever you want';
include 'user.php';
You're going about it backwards. The only URLs your code should be outputting are the 'friendly' ones. Those are the urls that will appear in the produced HTML and what will show up in the user's address bar.
e.g.
Bad URL (URL #1)
This URL is fine (URL #2)
You should never output anything but URL #2's. It'll be your server's responsibility to convert that clean (and in real terms, non-existent) URL to whatever really is on the server. PHP itself should never care nor see the /user/foo URL. PHP will be invoked as /user.php?id=foo as usual, and go about its business as usual.
The remote user would never see that rewriting occurring, they'll just see a request go out for /user/foo.

Creating SEO canonical paths (none absolute)

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

.htaccess redirection with query string to human readable url

I have a url: www.abc.com/change.php.
Parameters value is passed in post through $_POST[param].
I want to convert it to MVC like url through .htaccess such that:
www.abc.com/change.php with $_POST["height"] redirects to www.abc.com/change/height
and:
www.abc.com/change.php with $_POST["width"] redirects to www.abc.com/change/width.
How should my .htaccess file be changed for it?
I'm pretty sure you can't redirect or rewrite based on the name of a post variable.
Actually that's with .htaccess. If you redirect with php, you can definitely do that but that's not what you asked.
I guess you should also be able redirect/rewrite on get variable names.

How to extract URL parameters on 404 Error in PHP

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&param2=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']

Categories