at the moment I have this url format:
www.domain.de/?paramter=value
with php I can access the value like this:
<? echo $_GET['paramter']; ?>
but I would like to rewrite the url to this format:
www.domain.de/paramter=value
How can I realize this with a htaccess line and how can I access it with php after rewrite ?
www.domain.de/paramter=value
is a pretty strange way of putting values. However, whenever a user enters such URL, you can make sure that the user lands on the correct page via below .htaccess on Apache server
RewriteEngine On
RewriteRule ^/?([^=]+)=(.+)$ /?$1=$2 [L,NC,R=302]
Demo: https://htaccess.madewithlove.be?share=53dc007b-3781-4d1d-80c6-5f0234cdf11a
This way, your PHP code is intact and nothing needs to be changed on the backend code. If you wish to make a permanent redirection, make R = 301.
please try this:
$g = $_GET;
$new_str = '';
foreach($g as $key => $value) {
$new_str .= "$key=$value";
}
echo $_SERVER['SERVER_NAME'] ."/". $new_str;
Using the following URL:
http://site.test/i.php?p1=1&p2=2&p3=3,
It will return:
site.test/p1=1p2=2p3=3
Basically this code loops through the $_GET parameters and then grabs the key and the value, turns them into a string and then write a final URL with the string. You can format the output as you wish.
Related
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']
As I know the only way to get a value from the url is to use the $_GET[]; . To do this the url must be of the format: http://domain.com/index.php?var1=value1
And then to do the following:
<?php
$value1 = $_GET['var1'];
echo($value1);
?>
What I really want is to get the value of the url in the format:
http://domain.com/value1
I know that is not a really php job, but while my index is index.php can I get the value1 from the url? Using something different rather than HTTP Get method.
Cheers.
[SOLUTION]:
I use the URL rewriting to make it work as #KevBot suggest. So what I did:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?value=$1 [L]
It's called URL rewriting. You need to modify .htaccess.
Here's a great tutorial:
How does one make a view contain without using the get variable in PHP
I am trying to display post/id like this
www.domain.com/view/1234
instead of
www.domain.com/view.php?id=1234
Assuming you have an Apache server with the mod_rewrite module enabled, you can create a new .htaccess file with the following content:
// First of all we want to set the FollowSymlinks option,
// and turn the RewriteEngine on
Options +FollowSymlinks
RewriteEngine on
// This is the rule that changes the URL
RewriteRule ^view/([^/.]+)/?$ view.php?id=$1 [L]
This would do exactly like you wanted and redirects any /view/123 to view.php?id=123.
This is done via webserver url rewriting.
Here's some references for Apache (using mod_rewrite):
Apache.Org
(http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/)
If what you're looking for is the ability to parse the uri, you can use this:
$uri = $_SERVER["REQUEST_URI"]
$parts = explode("/",$uri)
The last element in your $parts array should be your view id.
Drupal(1) contains the following function to do exactly this:
function arg($index) {
static $arguments, $q;
if (empty($arguments) || $q != $_GET['q']) {
$arguments = explode('/', $_GET['q']);
$q = $_GET['q'];
}
if (isset($arguments[$index])) {
return $arguments[$index];
}
}
(1) I do not advocate using Drupal, I merely use it as an example.
You need to rewrite the URL with htaccess and intercept it somehow using PHP and loading the file from somewhere.
Rewrite URL with mod_rewrite (i.e.: domain.com/23234234/0 to domain.com?id=23234234&nr=0)
Make a call to the database requesting the post with id 23234234
Show it to the user
Is it what are you looking for?
(based on a previous answer)
What i want to be able to do is if someone writes in the url bar
www.somesite.com/test
test is like the Username name so it will be www.somesite.com/Username
then page navigate to another and display data from the data where the username is the same
The page will navigate to another page and display data from he database
is this possible first of?
And this is what i have write now it not much, and i am also new to PHP
$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url2 = "http://www.somesite.com/"$databaseVar1=mysql_result($result,$i,"username");
if ($url) == ($url2)
{
header("location: http://somesite.com/viewcontent.php");
}
else
{
//not found
}
Am i Able to do something like this? Any help will be great-full thanks
In PHP this is not exactly possible, however, you can do the following:
www.somesite.com/index.php/test
Apache will open index.php and you can use something like $_SERVER['REQUEST_URI'] to navigate from there
You will either need to use a rewrite rule in .htaccess to redirect all requests to index.php. Index.php can then use
Or you can put a ? before the username. Then index.php will automatically get all requests. The $_SERVER['querystring'] will then receive the username.
Taken some assumptions (due to lack of complete source code in your question):
mysql connection is estabilished
proper query has been sent to mysql server
variable $i is set to meaningful value
www server is configured to rewrite requests to index.php
You only have to compare REQUEST_URI with "username" column value taken from database.
Header "Location: ..." must have capital letter "L".
And avoid usage of "==" operator when comparing strings in PHP, strcmp() function is more reliable - "==" operator tests identity not equality - it's useful to compare numeric values.
You can define the index.php as the application dispatcher and create an .htaccess file that will redirect the requests to it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
index.php:
$page = $_SERVER['REQUEST_URI']; //=> '/Users/show'
if ($page == '/some/page/') include('file_that_handles_this_request.php');
Or you could use this to create instances of classes and call methods:
$page = $_SERVER['REQUEST_URI']; //=> '/Users/show'
list($class, $method) = explode($page);
$object = new $class();
$object->{$method}();
you should use mvc architecture to get perfect as you want.
the url in mvc is like same as you want. You can call the method with the parameter from url directly. Here is a url pattern in mvc architecture -
www.yoursitename.com/controller/methodname/parameter
I have searched left and right.
And I am trying to find a script or a method on how I can store links in database and use them for redirection.
This is an E-commerce project and I am looking for something to manage product and system URL from database.
Something similar like magento does.
Basically anything after the domain say like domain.com/demo/12/my_iphone
so now demo/12/my_iphone will be sent to database for query and in databases there will be a destination URL which could be productlisting.php?searchstring=iphones
The user will see link domain.com/demo/12/my_iphone but actually he will be seing productlisting.php?searchstring=iphones
Basically demo/12/my_iphone = productlisting.php?searchstring=iphones
And tomorrow if the user wants to edit demo/12/my_iphone to demo/12/myiphone he can just do so using simple form which will update in the database.
How can I achieve this?
Use mod_rewrite in apache in combination with a PHP script.
1) Make a .htaccess file and put it in a folder, say www.yourdomain.com/a/.htaccess
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteRule .* index.php?%{QUERY_STRING} [L]
2) Create an index.php that handles all requests
(psaudo-code)
<?php
/**
* This script is simply parsing the url used, /demo/12/my_iphone
*/
require_once('library.php');
$request_uri = $_SERVER['REQUEST_URI'];
// parse $request_uri by splitting on slashes /
$params = my_parse_function($request_uri); // <= parse_url() is helpful here!
$param1 = $params[0]; // "demo"
$param2 = $params[1]; // "12";
$param3 = $params[2]; // "my_iphone";
// db-lookup based on the request
$url = fetchUrlFromDB($param1, $param2, $param3);
if($url){
header('Location: '.$url); // $url = "productlisting.php?searchstring=iphones"
die();
}
else{
echo "invalid parameters";
}
?>
It wouldn't be difficult to program such a solution if it comes to that. You could grab the uri from $_SERVER['REQUEST_URI'] and pass it to parse_url to grab the path. Then use the path to query against the database.