Dynadot โ€” .com Registration $8.99

Quick htaccess mod_rewrite question

Spaceship Spaceship
Watch
Impact
98
I am trying to do the following:

www.domain.com/play/Pacman

will go to

www.domain.com/play.php?game=Pacman

I want to then on play.php have a variable $game that I can use to search a MySQL database for the gamename and display the appropriate details.


Reading some tutorials I have the following but it is not working for some reason:

Code:
RewriteEngine on

RewriteRule ^game/([^/]*)\.html$ /play.php?game=$1 [L]

Any help? Also, how do I call the $game variable on the play.php? Do I need to do a $thename=$_GET[] ?? htaccess is not one of my strong points and I have been reading tutorials for over an hour without any luck.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
I would have done it this way:
Code:
RewriteEngine on
RewriteRule ^play/([a-zA-Z0-9]+)/?$	/play.php?game=$1 [L]

([a-zA-Z0-9]+) - this will be substitution for $1 (game name)
[a-zA-Z0-9] - allowed characters in game name, you may alter this set if you intend to use another characters;
+ means that there can be more than one character (word);
/? - for both '/play/Pacman' and '/play/Pacman/' variants.;

Then, all you need is to get game name in your play.php file:
Code:
$gameName = isset($_GET["game"])?$_GET["game"]:"";

You should also sanitize $gameName variable to avoid SQL-injections before passing that variable to MySQL query.
 
0
•••
[a-zA-Z0-9]

So if I want a space, ! or - or ' I would make it

[a-zA-Z0-9!'- ] ??? (note the space before the end bracket)?
 
0
•••
So if I want a space, ! or - or ' I would make it [a-zA-Z0-9!'- ] ???

Well, almost :)

There are some tricks: in this particular case space should be escaped with \ and - should be the last character in []

So, finally it will look like this:
Code:
[a-zA-Z0-9!'\ -]

However, if you don't need such detailed control of input, you may use following rule:

Code:
RewriteRule ^play/(.+)/?$	/play.php?game=$1 [L]

Here . means 'any single character (including space) except end of line'
And + means 1 or more characters defined by .

If you need more information about regular expressions in .htaccess, you can follow this link.
 
1
•••
Thanks repped.
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back