friendly url don't recognize variable - php

I use in my .htaccess
RewriteRule ^profiles/([A-Za-z0-9-]+)/$ /profile.php?city=$1 [NC]
Works normally when accessing the url:
https://site.com.br/profiles/londres
But in the page, after insert an variable get, the php recognizes only the variable configured in htaccess, example when accessing the url:
https://site.com.br/profiles/londres?s=username
print_r($_GET);
return
Array ( [city] => londres)
that is, it does not return the variable s in GET

Use QSA flag in your rule to get query string as well.
RewriteRule ^profiles/([A-Za-z0-9-]+)/$ /profile.php?city=$1 [NC,QSA]
Reference:
https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

Related

Send values to another page without showing parameter at the end of url

We can get values from url like this.
abcd.com?id=123
We can get this id in php using
$id = $_get['id'];
But I see many urls like
abcd.com/123
How to do it and receive it in php page.
create a .htaccess file in webroot directory with
For numeric ID:-
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1 [L]
For alphanumeric ID:-
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ index.php?id=$1 [L]
Then, you can access domain.com/id as get parameter in index.php file
for that you have multiple choices :
Use a form and get params by POST,
Or you can use the PHP_REQUEST_URI by parsing it and get the member you want, for example if your URL is like http://mywebsite.com/users/125, in that php global variable, you will find an entry as an array that will contain members of your URL ( [0] -> ... [1] -> ... ).
Or, if you can use a Framework for examle Symfony, you will configure your routes and that's it.
Good Luck

QUERY_STRING only displays first parameter

I can only access my 1st URL parameter. I think it is because I have ampersands both in my RewriteRule as well as my QUERY_STRING. How can I access each parameter correctly in my PHP script?
I am making a request to
http://example.com/subdir/api/transport/1/car?type=fast&color=blue"
In my .htaccess file I have the following line:
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&params=%{QUERY_STRING}&api=$1 [L]
In my PHP, displaying json_encode($_GET) produces:
{"q":"\/subdir\/api\/transport\/1\/car","params":"type=fast","color":"blue","api":"transport\/1\/car"}
You will notice that the "color":"blue" no longer has an equal sign and is not contained within "params".
If I therefore display `json_enocde($_GET['params']) all I get is the following:
"type=fast"
What can I do to acquire the full set of parameters? Is it an issue with my .htaccess or my PHP file?
You don't need to use %{QUERY_STRING} in target by using QSA flag that appends existing query string to target if you're modifying query parameters.
You can just use your rule as:
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&api=$1 [L,QSA]
Well, if you think about the transformation that takes place:
index.php?q=%{REQUEST_URI}&params=%{QUERY_STRING}&api=$1
becomes:
index.php?q=/subdir/api/transport/1/car&params=type=fast&color=blue&api=transport/1/car"
So, of course you are getting that for your $_GET.
What I would do is change the rewrite to:
index.php?q=%{REQUEST_URI}&%{QUERY_STRING}&api=$1
And then in your code you will get:
{
"q":"\/subdir\/api\/transport\/1\/car",
"type": "fast",
"color":"blue",
"api":"transport\/1\/car"
}
If you want the params, you can then just remove the q and api keys from your $_GET.

can I pass variables through URL while using htaccess RewriteRule?

I was wondering if it is possible to pass another variable through URL while already passing a variable using .htacces?
for example,
RewriteRule ^browse/([a-zA-Z-]+)/?$ browse/index.php?brand=$1
lets say, website.com/browse/index.php?brand=louis-vuitton
was rewritten into website.com/browse/louis-vuitton
is it possible for me to use another variable like this:
website.com/browse/louis-vuitton/?sort=name
Yes it is, you simply add [QSA] After the rewrite condition
RewriteRule ^browse/([a-zA-Z-]+)/?$ browse/index.php?brand=$1 [QSA]
http://wiki.apache.org/httpd/RewriteFlags/QSA

How to get values from query string and pass it to new page using .htaccess file

currently I m using following code in my site in .htaccess file :
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L]
This redirects user to comman.php page say, user requests
http://www.mysite.com/myfolder/page1.php
will redirects to
http://www.mysite.com/myfolder/comman.php?cat=page1
This works fine. My question is how can I achieve following
http://www.mysite.com/myfolder/page1.php?var1=123
to redirect
http://www.mysite.com/myfolder/comman.php?cat=page1&param=123
i.e. whatever value passed to url using get method to add in my new url.
Thanks in Advance.....
You should add the QSA flag:
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L,QSA]
QSA stands for Query String Append. Anything after the ? in the original URL will be appended to the rewritten URL.
You need to ass QSA to your rule, i.e.
RewriteRule ^(.*)$ /index.php?pageid=$1 [QSA,L]
You can use the QSA flag in your RewriteRule for that.
See the docs.

How to get the third string from URL using php

I am using below code in htaccess
RewriteRule ^/?service/abc/([\w-]+)/?$ xyz.php?name=$1 [L,QSA,NC]
RewriteRule ^/?service/pqr/([\w-]+)/?$ xyz.php?name=$1 [L,QSA,NC]
to display my URL something like:
http://example.com/service/abc/review
http://example.com/service/pqr/review
which is working for me. Now my issue is, I have to get abc or pqr a string from URL and display
I am trying to get the name like
$getURL=explode('/', $_SERVER["REQUEST_URI"]);// getting service/abc/review
echo $a = next($getURL); //it's displaying only service
Or is there any best way?
Woult you help me out ?
Use one rule, allow for either abc or pqr in that position, and append it as an additional GET parameter:
RewriteRule ^service/(abc|pqr)/([\w-]+)/?$ xyz.php?service=$1&name=$2 [L,QSA,NC]
And use $_GET['service'] to access the value then.
you can edit the htaccess rules in this way
RewriteRule ^/?service/(.*)/(.*)/?$ xyz.php?endpoint=$1&name=$2 [L,QSA,NC]
and retrieve them in xyz.php file
var_dump($_GET);

Categories