limitphp Posted December 16, 2008 Share Posted December 16, 2008 I'm trying to do a url rewrite. I want to transform this: mysite.com/artist.php?artistID=2&artistName=The Strokes into this: mysite.com/artistName/The-Strokes/ Will modifying the htaccess file do this? Would I have to make some type of rewrite rule? RewriteRule ^artistName/([a-zA-Z0-9 ]+)/$ /artist.php?artistID=$1 Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/ Share on other sites More sharing options...
gevans Posted December 16, 2008 Share Posted December 16, 2008 Yes, it would be similar to the following RewriteRule ^artistName/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ /artist.php?artistID=$1&artistName=$2 Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716676 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 Also, when I do the url rewrite, will the querystring still be passed? Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716685 Share on other sites More sharing options...
cytech Posted December 16, 2008 Share Posted December 16, 2008 The query string gets rewritten at the end of the rewrite "/artist.php?artistID=$1&artistName=$2" Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716699 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 Ok, I put that rewrite in the .htaccess file. RewriteRule ^artistName/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ /artist.php?artistID=$1&artistName=$2 And I clicked on the link: http://localhost/greckle/artist.php?artistID=1&artistName=The%20Strokes and it took me to the artist.php page, but nothing happened. The link stayed the same and when I tried to grab the artistName: $artistname = $_GET['artistname']; it didn't work. As in, when I displayed it: echo $artistName nothing appeared. Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716711 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 I'm starting to understand.....in my website, I should write the url like this: mysite.com/artistName/The-Strokes/ not with the querystrings..... : ) I had it backwards......I kept thinking it was going to take my url from my website with the querystrings and rewrite it without........ No.... it takes the nice looking one and converts it to the appropriate one (secretly)..... : ) I'm starting to get this.... Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716723 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 Ok, if I rewrite a url like this: mysite.com/artist/The-Strokes and my rewrite rule is this: RewriteRule ^artist/([a-zA-Z0-9-]+)/$ /artist.php?artistName=$1 in the database, the name is "The Strokes", not "The-Strokes" with a dash. Will it match up? Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716789 Share on other sites More sharing options...
cytech Posted December 16, 2008 Share Posted December 16, 2008 Hey limitphp, No it will not match up, a quick fix would be to run the artistName through a "cleaning" function. Basically remove - and replace it with a space. $artistName = str_replace("-"," ", $artistName); (use the above code before you search artistName in your database). Normally though you should have whats known as a hash for each artist and save it in your database. So for example "The Strokes" would have a hash like "The-Strokes" and this is what you would use to check to see if theres a match. Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716792 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 Hey limitphp, No it will not match up, a quick fix would be to run the artistName through a "cleaning" function. Basically remove - and replace it with a space. $artistName = str_replace("-"," ", $artistName); (use the above code before you search artistName in your database). Normally though you should have whats known as a hash for each artist and save it in your database. So for example "The Strokes" would have a hash like "The-Strokes" and this is what you would use to check to see if theres a match. A hash for each artist? So, in my artist table, which contains artistID, artistName, etc.... add artistNameHash and it will be exactly like the artistName only it will have dashes if there are spaces? And if there aren't any spaces, it will be exactly the same as the artistName? Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716808 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 Ok, I added the hash entry to the artist table. the link now looks like: mysite.com/artist/The-Strokes/ and the rewrite looks like: RewriteEngine on RewriteRule ^artist/[a-zA-Z0-9-]+/$ /artist.php?artistName=$1 but when i click on the link I still get a 404 not found error. As if, the page doesn't exist. Does anyone know what could be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716823 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 ok, I tried chaning the rewrite to: RewriteRule ^artist/([a-zA-Z0-9-]+)/$ /artist.php?artistName=$1 still no luck. Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716827 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 ok, tried changing the rewrite to: RewriteRule ^artist/([a-zA-Z0-9-]+)/?$ /artist.php?artistName=$1 [NC,L] added the ? mark.... still no luck.....404 error not found still. Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716832 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 Can anyone help me? I know the mod_rewrite is on....because it worked before. I had some other really simple rule on to test it out....and it worked. Is there something wrong with the rewrite rule? Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716888 Share on other sites More sharing options...
limitphp Posted December 16, 2008 Author Share Posted December 16, 2008 ok...i took out the last backslash. Now it works! Thank you limitphp! By the way, what does the question mark do? Quote Link to comment https://forums.phpfreaks.com/topic/137198-mod_rewrite-help/#findComment-716895 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.