How to generate friendly URLs with .htaccess – part 2

Once you read part 1, you should know how to have a friendly url to play a game in your Flash game portal.

In the previous example, I showed you how to transform this link:

http://www.triqui.com/play.php?id=1713

into this one:

http://www.triqui.com/id/1713/

playing with .htaccess file

This is called “friendly” url, and now I’ll show you how to improve it. Starting from…

Your database game table

There are thousands of ways you can store your games data into your database, but all should follow a rule that in the table that stores your games information you have a column where you saved the game name.

No matter if you called the table “games” or “entries” or “bananas” and you called the column “game_name” or “title” or “apples”… you should have a table with all games with a column for the game name.

We can say the “game_title” column is a text one (or something that works in the same way) where basically you save stuff like this one:

Jamag: A simple one-word game name

Pillage the Village: A more-than-one-word game name

~TimedHouse~: A game name with one or more special chars, where in this example I am calling “special char” every character that is not an alphanumeric one

As every game developer is free to choose the name for his game, we must convert it in our standard way to name games.

There is not a right way or a wrong way, just choose your way.

I’ll clear this concept with an example. Let’s Google for a popular game like Desktop Tower Defense and you will notice that, as example, Kongregate’s urls is this one

http://www.kongregate.com/games/preecep/desktop-tower-defense-1-5.

while Jay is Games’ url is

http://jayisgames.com/archives/2007/06/desktop_tower_defense_15.php.

It’s easy to see that the first one coded the spaces with a - whine the second one coded the spaces with a _

So it’s time to introduce you my own rule:

All non-alphanumeric characters will be removed, with the only exception of spaces that will be changed with a -. The string will be lowercased too.

No matter the rule you will follow, you will have to create a new column in your game table that will contain the “translated” name.

The following picture will show the “game_name” and the “translated_game_name” columns for the last 30 entries

once you updated your database, it’s now time to update your…

.htaccess file

In the previous example, our .htaccess file was

ErrorDocument 404 /index.php
RewriteEngine on
RewriteRule ^id/([^/\.]+)/?$ /play.php?id=$1 [L]

Now it’s time to add a line in this way:

ErrorDocument 404 /index.php
RewriteEngine on
RewriteRule ^id/([^/\.]+)/?$ /play.php?id=$1 [L]
RewriteRule ^game/([^/\.]+)/?$ /play.php?game_url=$1 [L]

Line 4, the new one, acts in the same way as line 3, explained in part 1, just waiting for the game string to redirect to
play.php?game_url=translated_game_name

and that’s all for the .htaccess

Now, you can play Jamag in four ways:

1) http://www.triqui.com/play.php?id=1713

2) http://www.triqui.com/id/1713/

3) http://www.triqui.com/play.php?game_url=jamag

4) http://www.triqui.com/game/jamag/

As long as your play.php page detects if you are passing the id or game_url variable.

I did this way.

Hope you will find it useful

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers