Well, I want to have a redirect script on my server.
I want it to take the text after the URL, and redirect it to a website.
The one I have right now looks like this:
<?php
$url = $_GET['url'];
header("Location: http://youtube.com/watch?v=".$url);
?>
That makes the URL looks like : http://mywebsite.com/?url=YOUTUBE CODE HERE
But, I don't want it to look like that, I want the URL to look like this : http://mywebsite.com/YOUTUBE CODE HERE
and that will redirect the user to the YouTube video.
Thanks
to achieve that, you need to use .htaccess and mod_rewrite on apache, or a similar technology.
basic examle:
/.htaccess:
RewriteEngine on
RewriteRule ^v/(.*)$ v/index.php?url=$1 [L]
/v/index.php:
<?php
$url = $_GET['url'];
header("Location: http://youtube.com/watch?v=" . $url);
?>
Look into URL rewriting. Here's a tutorial on how to do it on Apache servers:
http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/
If you don't want to use mod_rewrite for such a simple task much quicker solution is
$url = $_SERVER['REQUEST_URI'];
Just trim the URL from things you don't need.
With PHP itself its possible in this way:
http://www.mywebsite.com/your-php-file.php?{YOUTUBECODE}
<?php
foreach ($_GET as $tmp=>$null) {
$redirect = $tmp;
break;
}
header("Location: http://www.youtube.com/watch?v=$redirect");
Related
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.
I want to create a link which is catched by $_GET in PHP. Let me explain:
Currently i do this in HTML:
Contact Us
And this is what i do in php:
<?php
if (isset($_GET['contactus'])){
include "contactus.php";
}
?>
In this case the address becomes like this www.domain.com/?contactus.
But I want the address like this: www.domain.com/contactus
If i change html to Contact Us
but it doesn't work. Anyone please tell me. Thanks
you need a .htaccess file to make it work
like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^contactus\/?/?$ index.php?contactus
</IfModule>
and index.php like this
<?php
if (isset($_GET['contactus'])){
include "contactus.php";
}
?>
You can use domain.com/contactus and parse the URL
$url = "http://domain.com/contactus";
$url = parse_url($url);
$path = $url['path']; // contactus
echo $path; // contactus
You can then use it
if ($path == "contactus"){
include "contactus.php";
}
You will still need URL rewrite, try adding or editing your .htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^contactus/?$ contactus-page.php [NC,L] # Handle requests for "contactus"
hi you need to do like this
at page where u set anchor tag do following
$url="contact.php?page"='contactus';
give href="$url" .
And other side you can use $urlget = $_GET['page'];
plz note the attribute u used is page so when u try to get in nother page u need to use $_GET['page'] and you can see url when you click on contact us page in your address bar there is also contact.php?page='contactus' showing
Please some one explain to me how it works and how to do it.
PHP has a function just for this: parse_url().
$parts = parse_url('domain.com/THESTRING');
$path = $parts['path'];
That wil get the part that you want
Could also use below if you want the current URI:
$_SERVER["REQUEST_URI"]
Yet another way:
echo strstr(str_replace('://', '', 'domain.com/THESTRING'), '/'); // THESTRING
EDIT: This should get you started.
Make sure your server support Mod_Rewrite
I opened a .htaccess in the public_html directory.
Then turned on the Mod_Rewrite by this command:
RewriteEngine on
Then I write a new law:
RewriteRule (.*)\.html$ index.php?link=$1
Thats bicycly mean that every url that ends with .html will redirect to index.php
Then in index.php I get the URL link
How do i get the id of the user from seo friendly url?
Here's the url (1 is the id)
http://www.website.com/users/edit/1
And, if the url is http://www.website.com/users/edit/1/stack-overflow/ in this case how do i find the id?
I am trying to get it as <?php user = User::find($_GET['id']); ?> but id doesn't work.
<?php
$url = explode("/",($_SERVER["REQUEST_URI"]));
print_r($url);
?>
In your case $url[2] should have the ID that you are after...
The best way to accomplish this is using .htaccess and mod_rewrite to assign the $_GET variables:
.htaccess:
RewriteBase /
RewriteRule ^users/edit/(.*)/(.*)$ users/edit/?user_id=$1&somethingelse=$2 [L,NC]
Then in your php script:
$user_id = $_GET['user_id'];
$somethingelse = $_GET['somethingelse'];
Uhm... I think you should tell us more about the class or framework you are using... But whatever you are doing can't work at all. Because $_GET would only contain something if you would actually pass parameters in the ?id=123 way in the url... So this find will search for nothing because $_GET is empty.
Parse the super global $_SERVER['REQUEST_URI'], which will contain something like users/edit/1/stack-overflow/, with regex or substr and strpos
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.