Generate URL Alias?? in PHP - php

I just saw this somewhere, and I'm interested on it, and can't seemed to find it anywhere or I just used the wrong words to search for.
Well I saw this link,
http://splur.gy/r/QqVYf/r/2tgNklHgmVK
and when I clicked it, I got redirected to other page which called
https://www.facebook.com/xxx.xxx?sk=app_xxxx
Anyone knows how this thing was made? or just a little hint to start off?
A help would be nice. :)

These are done with RewriteRule, a simple Google search willgive you mroe details.
In short, the URL will be broken down sorta like this: (Line 1, URL part, Line 2, PHP relative.
http://splur.gy
http://splur.gy/index.php
r
$_GET['var_1']
QqVYf
$_GET['var_2']
r
$_GET['var_3']
2tgNklHgmVK
$_GET['var_4']
The RewriteMod will take the URL as setup in the above format, and pass the varialbes to a script. It is another way of posting variables in the URL.
As you see above: stackoverflow.com/posts/15182831, does not actually have a file named posts/15182831, it is simple used as a variable, passed to a script which queries that database, and spits out results based on what the script says.

You will need to have a server that will allow you to rewrite requests so you can redirect all requests to a single script. If you are running Apache, you would create an .htaccess file with something like this in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^r$ /redirect.php [L,QSA]
RewriteRule ^r/(.*) /redirect.php?__q=/$1 [L,QSA]
</IfModule>
Then if you go to http://yourdomain.com/r/234243/adsfsd, the request will be sent to the script /redirect.php and '234243/adsfsd' will be passed as the GET paramiter 'q'.
Then you would create a file called redirect.php that would process the request and then redirect the user. It might look somthing like this:
<?php
$redirection = process_to_determine_location_from_query( $_GET['q'] );
header( 'Location: {$redirection}' );
?>

It's called a redirect. You can do it in PHP with this code:
<?php
header('http://example.com');
Another thing that might have happened is that the link you saw was not the actual link you follow when you click. It's as simple as doing this:
example.com

Anyone could do that.
http://www.google.com/
It has nothing to do with PHP.

Related

PHP MVC - Database routing?

Hi I'm currenly playing around with PHP MVC programming, and was wondering if anyone has made some sort of "routing" with a database?
I have a "page" table that looks like this: http://i.imgur.com/xS1OvjW.png
Currently all routes are hardcoded, and I thought it must be possible to do with a database..
But not only do I want to get the page and show it, I also need to be able to send parameters with it.
Example:
As shown in my page table, I have a "test" url. If i type http://demo.com/test/ I would get "rerouted" to use the "home" controller and "Index" method. But I also need to be able to type http://demo.com/test/id/40 and id/40 will be sent as params to the controller/method.
If this isn't a good thing to do, or if anyone got a better soloution please let me know! :)
Regards,
Frederik
This definitely depends on the server you're using, but since you're a PHP MVC noobie I'll reference apache in this example, and hope it's what you have for the sake of the examples.
First, you'll need your webserver configured to know that it has to send all traffic through your base page (usually index.php). Now that page would do some other stuff (call bootstrap, etc) but for the sake of argument we'll say that all it does right away is look at the request from the page, compare it to the DB, and complete the request if it can.
In that case, it will be helpful to have the request info from the server passed in to the index.php page. To do this, you'll want to configure apache with an .htaccess file similar to:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]
This tells it to use index.php for all requests that aren't specific files or directories, and to pass the full url path through to index.php as a $_GET var.
Next, in index.php, you'll want to check the path you were passed against DB of paths. Here's a really simple example to show:
<?php
// Obviously use your database and some string parsing here to match correctly
// I generally explode the path on '/' to break it into controller, action, etc.
if ($_GET['path'] == "user/account") {
// Then you call the controller that matches the first part of the route
// The action that matches the second part of your route
// And pass the request along so you can access anything else
call UserController::accountAction($_REQUEST);
} else if ($_GET['path'] == "user/resetpassword") {
call UserController::resetPasswordAction($_REQUEST);
}
From there, you should be in the right place and have everything you need. That Controller/Action URL format is a fairly common one for how easily it lets us do this.
Hope the answer helped!

Short URL with htaccess on many subfolders with same file

I have this situation:
There are many scripts that can be accessed like this:
http://website.com/script1/
http://website.com/script2/
Each script has a display.php file that receives a variable from the index.php file VIA a link. I'm using the $_GET method to reat that variable.
Example:
http://website.com/script1/display.php?id=124
What I'm trying to do is to access the display page like this:
http://website.com/script1/124
http://website.com/script2/124
Does anyone have any suggestions? My experience with htaccess is ~0.
RewriteRule script[\d]+\/[\d]+ /script$1/display.php?id=$2
This would rewrite anything like script345987/22352 to script345987/display.php?id=22352
Edit if you want to match anything, not only script, then:
RewriteRule (.*)\/[\d]+ /$1/display.php?id=$2

mod_rewrite from URL to GET variable

If someone tries to access this page:
www.myhomepage.com/m/40921
I want the URL (above) to remain the same but the content should be from this page:
www.myhomepage.com/msg?t=40921
If it must be exactly like you said, then use this
RewriteRule ^m/([0-9]+)\/?$ msg?t=$1
But I think you made a typing error. Shouldn't it be msg.php? If so, use this:
RewriteRule ^m/([0-9]+)\/?$ msg.php?t=$1
And in your php file, you can get the t with $_GET['t'].

Adding a RewriteRule for a PHP page with special handle requests

In my php page I have a hyperlink like:
Remove User";
The delete_user_page.php via GET takes the name of the user and executes the script. I want a URL rewrite mechanism inorder to hide the passed variable. So I wrote:
.htaaccess
RewriteEngine On #Turn on the re-writing engine
RewriteRule ^delete/?$ delete_user_page.php?name=$1 [NC,L] # Handle requests for "Delete users page"
When loading this onto the server and refreshing the results nothing has changed/showed so I realized I am doing something wrong.
Where does htcaccess have to be located relative to delete_user_page.php?
Looking at "delete_user_page.php?name=$1", is $1 correct or should it be different?
If there is something else wrong in my script please tell me.

$_GET URL ReWriting

Hey everyone. Hopefully I can explain this correctly.
I have some URL's which I would like to tidy up through URL reWriting. For instance, I have:
domain.com/index.php?page=1
domain.com/index.php?page=2
domain.com/index.php?page=3
domain.com/index.php?page=4 etc..
which I would like to be shown in the URL as:
domain.com/page/1
domain.com/page/2
domain.com/page/3
domain.com/page/4 etc..
Also a quick question:
-Will the $_GET query's still be executed if the URL is rewritten?
Many thanks ahead of time. I have the modules enabled and the .htaccess is ready to go. i just need some direction. Thanks!
Add this to your .htaccess file:
RewriteEngine On
RewriteRule ^page/(\d+)/?$ /index.php?page=$1 [L]
The GET queries will always be executed. If the user types in domain.com/index.php?page=3, then index.php will run. If they type in domain.com/page/3, then mod_rewrite will convert it to domain.com/index.php?page=3 before passing it to PHP. Either way, index.php is run.

Categories