Home Rewrite profile.php?user=username to profile/username
Reply: 2

Rewrite profile.php?user=username to profile/username

emma Published in 2017-12-05 10:44:19Z

I've created a very basic php templating system that I'm using to display a page with this URL: /basedir/index.php?page=home

The home content is served by the script home.php.

With a rewrite rule in .htaccess, I want my url to look like this: /basedir/home.

The problem that I'm having is that I've got stuck when it comes to rewrite one more GET variable that I'm using to display a user's profile. For now the profile page url looks like this: /basedir/profile?user=username.

Now I want it to look like this: /basedir/profile/username.

So far, my .htaccess looks like this:

RewriteEngine On

RewriteBase /basedir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) ?page=$1 [L,QSA]

I have no idea if this is good or if i have to write a completely new .htaccess file.

LGT Reply to 2017-12-05 12:06:53Z

Based on your code, and my limited understanding, I would guess something like this:

RewriteRule ^profile/([^?]*) ?page=profile&user=$1 [L,QSA]

.. insert it above your RewriteRule ^([^?]*) ?page=$1 [L,QSA].

anwerj Reply to 2017-12-05 12:14:53Z

As you can see at http://htaccess.mwl.be/ your url http://www.example.com/profile/username is converted into http://www.example.com/basedir/?page=profile/username

Now what you need to do is put index.php in basedir where explode the page and retrieve username.

$page = $_GET['page'];
$parts = explode('/', $page);

    case 'profile':
             $username = $parts[1];
             return doWhatEverYouWantToDoWithUsername($username);
        return handleJustProfilePage();

        return yourDefaultHandler();


This approach is called routing you can make way better with some external routing libraries.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.617048 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO