NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Quick htaccess mod_rewrite question

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
6 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 05-23-2011, 03:32 PM THREAD STARTER               #1 (permalink)
jdk
Senior Member
 
jdk's Avatar
Join Date: Jul 2004
Location: Florida
Posts: 1,496
jdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond repute
 



Help! Quick htaccess mod_rewrite question


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.
????: NamePros.com http://www.namepros.com/programming/715966-quick-htaccess-mod_rewrite-question.html


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.
__________________
All offers are valid for 12 hours unless otherwise stated.
Last edited by jdk; 05-23-2011 at 03:55 PM.
jdk is offline   Reply With Quote
Old 05-23-2011, 04:04 PM   #2 (permalink)
NamePros Member
 
un4given[MAD]'s Avatar
Join Date: Aug 2008
Location: Solar System \ Earth \ Ukraine \ Kiev
Posts: 43
un4given[MAD] is on a distinguished road
 



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)
????: NamePros.com http://www.namepros.com/showthread.php?t=715966
[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.
__________________
Tired playing Freecell, Klondike or Spider? Discover 800 solitaire games at SolitairesUnlimited.com and play'em all!
un4given[MAD] is offline   Reply With Quote
Old 05-23-2011, 04:56 PM THREAD STARTER               #3 (permalink)
jdk
Senior Member
 
jdk's Avatar
Join Date: Jul 2004
Location: Florida
Posts: 1,496
jdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond repute
 



[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)?
__________________
All offers are valid for 12 hours unless otherwise stated.
jdk is offline   Reply With Quote
Old 05-23-2011, 05:21 PM   #4 (permalink)
NamePros Member
 
un4given[MAD]'s Avatar
Join Date: Aug 2008
Location: Solar System \ Earth \ Ukraine \ Kiev
Posts: 43
un4given[MAD] is on a distinguished road
 



Originally Posted by jdk View Post
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.
__________________
Tired playing Freecell, Klondike or Spider? Discover 800 solitaire games at SolitairesUnlimited.com and play'em all!
un4given[MAD] is offline   Reply With Quote
Old 05-23-2011, 06:15 PM THREAD STARTER               #5 (permalink)
jdk
Senior Member
 
jdk's Avatar
Join Date: Jul 2004
Location: Florida
Posts: 1,496
jdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond reputejdk has a reputation beyond repute
 



Thanks repped.
__________________
All offers are valid for 12 hours unless otherwise stated.
jdk is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:48 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger