URL redirect to GET variables - php

In a PHP website I want to set a redirect if there are variables in the URL inside a specific folder (.com/promo/).
When a user visits:
www.example.com/promo/Marco-Aurelio
Redirect to:
www.example.com/promo/?user=Marco-Aurelio
What PHP code or htaccess rules would you use?

function RedirectToFolder(){
//lets get the uri
$uri = $_SERVER["REQUEST_URI"];
//remove slashes from beginning and ending
$uri = trim($uri,"/");
//lets check if the uri pattern matches the uri or not
//if it doesnt match dont continue
if(!preg_match("/promo\/(.+)/i,$uri)){
return false;
}
//lets now get the last part of the uri
$explodeUri = explode("/",$uri);
$folderName = end($explodeUri);
//lets get the new url
$redirectUrl = "http://www.example.com/promo/?user=$folderName";
Header('Location:'.$redirectUrl);
exit();
}//end function
So just call the function after the php opening tag
RedirectToFolder();

Create .htaccess in the apache DirectoryRoot containing the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/promo/(?![?])(.+)
RewriteRule ^ /promo/?user=%1 [L]
So that the url
http://www.example.com/promo/Marco-Aurelio
Will be redirected to
http://www.example.com/promo/?user=Marco-Aurelio

Related

.htaccess rewrite to access php include in subfolder

I have this URL that give me two main parameters, folder and page, and som other tahat can variable to be present or not:
http://www.domain.com/adm/master.php?f=folder&p=page&otherparam1=somthing&otherparm2=somthing&otherparm3=somthing[...]
in my master.php-file I include a php-file based on these parameters:
$folder = $_GET['f'];
$page = $_GET['p'];
if(empty($page)) {
$page = 'dashboard';
}
$path = $folder.'/'.$page;
include_once 'pages/'.$path.'.php?otherparm1=somthing&otherparm2=somthing&otherparm3=somthing[...]';
The otherparam is to be multiple parameters given i the URL.
I want to rewrtite the URL to show somthing like this:
www.domain.com/adm/folder/page/somthing/somthing/somthing[...]
I have searched the web, but not been able to find a solution.
If you want to do it with simple .htacces then you can use following .htacces code:
RewriteEngine on
RewriteCond $1 !^(index\.php|css|favicon.ico|fonts|images|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Basically what this code do is overwrite your request to your domain (ex: localhost/mysite), instead accessing folder or file it will access your index.php. Except for index.php, favoicon.ico, robots.txt files and css,fonts,image and js folder or what you define in second line on my code above.
Try this in your Root/.htaccess
RewriteEngine On
RewriteRule ^adm/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /adm/master.php?f=$1&p=$2&otherparam1=$3&otherparm2=$4&otherparm3=$5 [NC,L]
$1 ,$2,$3... contains whateve value was matched in regex capture groups "([^/]+)".
This will rewrite
example.com/adm/folder/page/foo/bar/foobar
to
example.com/adm/master.php?f=folder&p=page&otherparam1=foo&otherparm2=bar&otherparm3=foobar
without changing the url in browser address bar.

URL redirection upon parameters

I have a url pattern -- www.domain.info/(unique_number)
for example : http://domain.info/1211.10/09879 where 1211.10/09879 is a unique number
now ,on GET request I want to redirect this url to page.php where page.php will display unique_number's data.
where should i code for getting unique number from url?(i dont want to make directory - 1211.10/09878/)
what is the best way to achieve this ?
To achieve this you must first configure the web server in order to send all requests to the same script, assuming you are using Apache this would be something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ routing.php [QSA,L]
This will send all requests that don't point to an actual file to the routing.php script.
Now in routing.php the identifier can be accessed through the global $_SERVER['REQUEST_URI'] variable:
// assuming the whole URL is http://domain.info/1211.10/09879?someParameter=someValue
$uri = $_SERVER['REQUEST_URI'];
// remove the leading / and parameters:
$uri = substr($uri, 1);
if (strstr($uri, '?') !== false)
{
$uri = substr($uri, 0, strpos($uri, '?'));
}
// Here $uri contains "1211.10/09879" and you can carry on

.htaccess not redirecting successfully for pretty url's

My .htaccess is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./backend-scripts/url_parser.php
and then, handling the url redirect is the file url_parser.php which is as follows.
<?php
// open file and get its contents in a string format.
$string = file_get_contents("../json/site_map.json");
// decode the json string into an associative array.
$jsonArray = json_decode($string, TRUE);
// add trailing slash to URI if its not there.
$requestURI = $_SERVER['REQUEST_URI'];
$requestURI .= $requestURI[ strlen($requestURI) - 1 ] == "/" ? "" : "/";
// split up the URL at slashes.
$uriArray = explode('/', $requestURI);
// select the last piece of exploded array as key.
$uriKey = $uriArray[count($uriArray)-2];
// lookup the key in sitemap
// retrieve the absolute file URL.
$absPath = $jsonArray[$uriKey];
// reformulate the URL.
$path = "../$absPath";
// include the actual page.
include($path);
?>
in order to test my php code, I replaced
$requestURI = $_SERVER['REQUEST_URI'];
by the following:
$requestURI = "/welcome";
and it worked perfectly. So I'm pretty sure that there is something wrong inside my .htaccess file. How can I change that?
Change:
RewriteRule ^(.*)$ ./backend-scripts/url_parser.php
to
RewriteRule ^(.*)$ ./backend-scripts/url_parser.php?url=$1
Then change $requestURI = $_SERVER['REQUEST_URI']; to:
$requestURI = (!empty($_GET['url']))
? $_GET['url']
: ''; // no url supplied
WARNING: Do not pass user supplied values to include(). Make sure the paths are checked against a proper whitelist, otherwise a malicious user can hijack your server.

HTACCESS not redirecting in server

This is my htaccess code:
RewriteEngine On
RewriteBase /visio
# Turn on the rewriting engine
# RewriteRule [region]?.html$ index.php/login/getProvinces [L]
# RewriteRule post_([0-9]+).html$ index.php?admin/index/url=$1
RewriteRule ^([^.]+)$ index.php/admin/index/$1
When iam try to load this url :
http://myserver.net/visio/test
I got the 404 not found error.In this url visio is the directory of my site in the server.When iam loading this url i want to go to the admin controller's index().
How can i do this?
If there is any mistake in my code?
This is mmy index() code:
function index(){
if(isset($_GET['url'])){
$newkey = $_GET['url'];
$data['result'] = $newkey;
$this->load->view('index',$data);
}else{
redirect('admin/index_login');
}
}
I want to get the url value in index() also.
But when iam changing the url like this:
http://myserver.net/visio/?test
Then it will goes to the index().So what is the problem in code?
^([^.]+)$ is a regular expression that matches "everything that is not any character". In other words, it will only match "nothing, once or more" (the dot symbol). If you meant to match actual dots, add a backslash in front of it.

URL Manipulation PHP

I have a url something like
http://something.com/abc/def/file.php/arguments
This simply executes final.php and /arguments is passed to $_SERVER['PATH_INFO'] variable.
I want to execute the same but without the '.php' i.e,
http://something.com/abc/def/file/arguments
I am guessing I need to add something to http.conf, or...?
.htaccess is your friend
Options +FollowSymLinks
RewriteEngine on
RewriteRule file/(.*) file.php?param=$1
I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.
In your htaccess use like:
<IfModule mod_rewrite.c>
RewriteEngine On
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as
class URI
{
var $uri;
var $segments = array();
function __construct()
{
$this->uri = $_SERVER['REQUEST_URI'];
$this->segments = explode('/',$this->uri);
}
function getSegment($id,$default = false)
{
$id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
return isset($this->segments[$id]) ? $this->segments[$id] : $default;
}
}
Use like
http://mysite.com/posts/22/robert-pitt-shows-mvc-style-uri-access
$Uri = new URI();
echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'
Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param
Hope this helps you move forward with your application.
So you want to rewrite your URL's. Have a look here: http://corz.org/serv/tricks/htaccess2.php
This URL style can be managed by the url_rewrite (called url rewriting) and it can be done with .htaccess file of your Apache server.
to do it you'll need to write this in you .htaccess file:
RewriteEngine On
RewriteRule ^http://something.com/every/name/you/like/(arguments)/?$ server_folder/page.php?argument_var=$1
The first block of code, rapresents the user called page:
^http://something.com/every/name/you/like/(arguments)/?$
The second block is the real page you want to call, where $1 is the var value inside the ()
server_folder/page.php?argument_var=$1
If the user must go to an URL where the arguments are numbers only you should insert:
^http://something.com/every/name/you/like/([0-9])/?$
If the user must go to an URL where the arguments are letters only you should insert:
^http://something.com/every/name/you/like/([a-zA-Z])/?$
To work correctly with this URL style you'll need to understand a little bit of regular expresions like in this link.
You could find useful this table to help you understand something more.
Note you can write different URL instead of the real page name like:
^http://something.com/love/([a-zA-Z0-9])/?$ section/love/search.php?$1
This should be useful to hide server pages.

Categories