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}¶ms=%{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}¶ms=%{QUERY_STRING}&api=$1
becomes:
index.php?q=/subdir/api/transport/1/car¶ms=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.
Related
I'm searching but I can't do work it.
I have the following .htacess
RewriteEngine on
# Rewrite request URL
# Input: index/VIDEO/
# Output: index.php?id=VIDEO
RewriteRule ^(\w+)/?$ index.php?id=$1
It work fine changing the following url:
https://subdomain.domain.com/path/to/index.php?id=234556
To
https://subdomain.domain.com/path/to/234556
But I've add a second parameter (license), so I need rewrite the following URL:
https://subdomain.domain.com/path/to/index.php?id=234556&license=23432532
To
https://subdomain.domain.com/path/to/234556/23432532
Or
https://subdomain.domain.com/path/to/234556&license=23432532
I've been trying multiple ways searching here but I cant do that this work.
You can use:
RewriteRule ^([^/.]+)/(.*?)$ /index.php?id=$1&license=$2 [L,QSA]
In which, first parameter holds all values until it finds /. Next parameter holds all. You can repeat first expression multiple times as well.
Alternate method:
But when your application needs some more search parameters that needs to be dynamically handled, you can make use of REQUEST_URI in PHP.
In that case, your RewriteRule can be like:
RewriteRule ^ index.php [L]
And in PHP, you can retrieve all the values using $_SERVER['REQUEST_URI']:
if (isset($_SERVER['REQUEST_URI']))
{
$params = explode("/", ltrim($_SERVER['REQUEST_URI'], "/"));
print_r($params);
}
See another SO Answer. In that way, other pages can utilize the same rewrite rule for different purpose.
I'have this rewrite condition that redirect all the request of non-existing files to the app.php in parent directory.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.*)$ %2../app.php [L]
In the last line (rewriterule) the value of $1 is the path relative to the dir where .htacces is, in order to make it portable ( found here: http://linlog.skepticats.com/entries/2014/08/Using_RewriteBase_without_knowing_it.php ).
That is : calling http://localhost/myapp/public/test123/r.txt I will get test123/r.txt in $1 variable (considering that .htaccess is in public/).
I would like to pass the value of $1 to the app.php, a common solution would be to append it as a query string:
RewriteRule ^(.*)$ %2../app.php?__path=$1 [L]
In this way I have __path as a GET variable (PHP: $_REQUEST["__path"]) , but how it works with other kind of requests like POST (query string should not be there) ?
A more clean solution would be to put $1 in a HTTP custom header but, how can I set a custom header, say MYAPP_PATH to $1 value through .htacess ?
To start with your comment:
I've noticed that the trick ?__path=$1 overrides all the querystring that I pass in the original URL.
You forgot to set the QSA flag.
Second to the core question:
In this way I have __path as a GET variable (PHP: $_REQUEST["__path"]) , but how it works with other kind of requests like POST (query string should not be there) ?
This just works fine. GET-parameters are just popularly called that, they're actually query string parameters and can be passed with any HTTP verb, from GET and POST to PUT and DELETE. So if you POST to a URL with query string parameters you can still read them from $_GET just fine.
Final point, as the above solves all your issues: please do not use $_REQUEST. It's really REALLY bad practice, and may lead to obscure security and stability issues. Just use $_POST, $_GET et al instead.
I would like to redirect URI into subpage.php and read is as parameter (by $_GET method).
Example 1:
http://homepage.com/wap/show_counties.php?id_county=51&char_county=&lang=en
into
http://homepage.com/controller.php?url=wap/show_counties.php?id_county=51&char_county=&lang=en (??? - can I pass url parameter like this?)
Example 2:
http://homepage.com/accommodation/hostel-star-bratislava
into
http://homepage.com/controller.php?url=accommodation/hostel-star-bratislava
Example 3:
http://homepage.com/en/search-in-page-12.html?ordering=newest&searchphrase=any&searchword=do%25252525252525252525252B700%252525252525
into
http://homepage.com/controller.php?url=en/search-in-page-12.html?ordering=newest&searchphrase=any&searchword=do%25252525252525252525252B700%252525252525 (??? - can I pass url parameter like this?)
EDIT: I would like to make url redirection by .htaccess
Could you help me please?
Regards
Jan Zitniak
You can do this with mod_rewrite. What you want to do can be described like so:
The request uri should be passed to a specific file in a get variable
If there is an original query string, it should be retained
You can do this with the QSA (query string append) flag in mod_rewrite. In addition to that, in the example below I add a condition that checks if the file that is requested does not exist. This prevents the rule from matching itself (controller.php exists) and prevents the rule from working on files that are typically not handled by a controller (js/css/images). Add the following to the beginning of the .htaccess in your www-root:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /controller.php?url=$1 [QSA,L]
See the documentation for mod_rewrite in general and the document about flags specific for more information.
I'm losing URL parameters when I do mod_rewrite, and I don't understand why...
I've had to add a country code to the URL, for localisation. So my old URL:
The original URL:
www.domain.com/mail_confirmation.php?id=222
now looks like
www.domain.com/us/mail_confirmation.php?id=222
Mod rewrite should call:
www.domain.com/mail_confirmation.php?id=222?country=us
And this is the rule I need help with. It doesn't do what I expect it to, and loses the parameters along the way:
RewriteRule ^([a-zA-Z]{2})/(.+)\?(.+) $2?$3&country=$1
Another rule that might be affecting is this one, at the very beginning of the file:
RewriteRule ^([a-zA-Z]{2})/?$ index.php?pais=$1
Do you see any mistakes here? I'd appreciate your help!
Your RewriteRule needs to be
RewriteRule ^([a-zA-Z]{2})/(.+)$ $2?country=$1 [QSA,L]
Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.
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¶m=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.