Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User friendly URLs - mod rewrite and php redirections

So far I've done this:

RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?load=$1 [QSA,L]

Then on my index page (in the root directory) I'm using PHP to determine which page to load:

// Swap to variables
    $load = $_GET['load'];

// Home page
    if (!$load || $load == "") { include('home.php'); exit(); }

// Dashboard
    if ($load == "dashboard") { include('dashboard.php'); exit(); }

// About
    if ($load == "about") { include('about.php'); exit(); }

// Username
    $data_array = array(':username' => $load);
    $select_account = $connect->prepare("SELECT * FROM `users` WHERE `username` = :username");
    $select_account-> execute($data_array);
    $account_amount = $select_account->rowCount();
    if ($account_amount > 0) { include('profile.php?name=$load'); exit(); }

// Redirect to 404 if there are no results
    include('404.php'); exit();

Everything so far is working but users can upload photos to a gallery and I want them to be viewed like so:

www.mysite.com/[username]/gallery/

But if you were to type that as the url the rewrite reads [username]/gallery/ as one section which means $load = [username]/gallery which would give me a '404'.

There is probably a better solution to getting the desired results but I'm not too good with the .htaccess and rewriting. I would like to add that I like this rewrite too since I have sub-directories called signup and signin which both have sub-directories in them too, but if I go to the URL:

www.mysite.com/signup
www.mysite.com/signin

It ignores the rewrite and goes to the directory instead of running it through the $load statements which is what I want.

Also, to note, on registering an account, any username which matches strings such as dashboard or about etc it doesn't allow them to use it, this stops usernames and the $load if/else statements and their includes being mixed up etc

EDIT

Another thing I forgot to note is since they can call the gallery whatever they like, it needs to do a search to see if that gallery exists, so for example:

www.mysite.com/username/my+first+album

It would first need to check the username exists, then check the album exists, then display it if it does or 404/redirect to wherever if it doesn't. So basically, both parameters/queries will be dynamic. Not only that but then individual photos within that album need to work the same, for example:

www.mysite.com/username/my+first+album/my+picture

I hope that makes sense...

like image 531
no. Avatar asked Feb 02 '12 01:02

no.


People also ask

What is the difference between rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.

How do you rewrite a URL in HTML?

To rewrite urls you need to configure the webserver to do that (for example in apache with . htaccess . But if you want to do that without use server configuration, a bad solution exists: make a folder with the name of the url and put the html into that with the name index. php .


2 Answers

A simple solution would be: EDIT HTACCESS

RewriteBase /
RewriteCond %{REQUEST_URI}  !/signup
RewriteCond %{REQUEST_URI}  !/signin
RewriteRule ^([^/]*)/([^/]*)$ index.php?load=gallery&username=$1&gallery=$2
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?load=$1 [QSA,L]

Now that PHP part ( for index.php ):

$load = $_GET['load'];
switch ($load){
    default:
        include('home.php');
        exit();
    break;
    case 'dashboard':
        include('dashboard.php');
        exit();
    break;
    case 'about':
        include('about.php');
        exit();
    break;
    case 'gallery':
        $username = $_GET['username'];
        $gallery = $_GET['gallery'];

        //check for the username and gallery

        header("Location: your-gallery-location-goes-here");
    break;
}

Hopefully it's gonna help :)

like image 189
Sal Avatar answered Sep 22 '22 17:09

Sal


What you want is what is known as an URL router, this requires you to analyze the url and make decisions based on the contents. Most systems do this by getting you to provide an url template, and a function to call if the url matches. The function is normally passed any sub-matches in the template url.

For example Django uses regexes for its url routing and passes the named matches as arguments to a given function (or class).

If this is too complex for your needs then you can just use specific regexes to parse the url, your gallery case would be:

$matches = array();
$re = "/\/([\w\d])+\/([\w\d+%])+\/?/";
preg_match($re, $load, $matches);
$username = $matches[0];
$gallery  = $matches[1];

you can then use $username and $gallery however you wish.

Note

The above assumes that it will match, you will need to check the return value of preg_match to make sure. Also, I have not checked the regex, it may be wrong, or use features not in this syntax.

Reference

  • Regular Expressions
  • PHP PCRE Function Documentation (PCRE = Perl-Compatible Regular Expressions)
like image 32
Aatch Avatar answered Sep 20 '22 17:09

Aatch