barney0o0 Posted July 25, 2009 Share Posted July 25, 2009 I have changed the htaccess file so this; website.com/whatson.php?event_id=1&cat_name=comedy&event_name=some ones name changes to this; website.com/whatson/1-some ones name.html using: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriterule ^whatson/([^-]+)-([^&]+)\.html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [L] </IfModule> So, if i add the link to the browser it works fine. One thing that i cant get my head around (and yes, ive been searching around for the last few days!)....what php code do i need to have to change the actual links in my webpage (below) to a seo friendly link? <a href="whatson.php<?php echo $row_main['event_id']; ?>&cat_name=<?php echo $row_main['cat']; ?> &event_name=<?php echo $row_main['eventname']; ?>" ...i hope someone could literally spell it out for me...thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/ Share on other sites More sharing options...
wildteen88 Posted July 25, 2009 Share Posted July 25, 2009 You change the link to your new SEO friendly url. So it'll be <a href="whatson/<?php echo $row_main['event_id']; ?>-<?php echo $row_main['cat']; ?>-<?php echo $row_main['eventname']; ?>" Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882649 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 it i-t it Say it 3 times and it's yours. Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882651 Share on other sites More sharing options...
barney0o0 Posted July 25, 2009 Author Share Posted July 25, 2009 You change the link to your new SEO friendly url. So it'll be <a href="whatson/<?php echo $row_main['event_id']; ?>-<?php echo $row_main['cat']; ?>-<?php echo $row_main['eventname']; ?>" Thanks for your quick reply...so now to part 2! How do i redefine the sent values (id's etc) in the next page for a mysql query... Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882656 Share on other sites More sharing options...
wildteen88 Posted July 25, 2009 Share Posted July 25, 2009 As you would normally would using the $_GET superglobal. The variables you'll need are $_GET['event_id'] $_GET['cat_name'] $_GET['event_name'] Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882658 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 Okay so the short answer is you can't. Not without passing them in the url as before. You can make the buttons into form submit buttons, with hidden fields passing, but that no longer really makes them links. Alternatively you could parse the new url for the info, since the new link is unique and contains the id, name, etc.. right? Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882659 Share on other sites More sharing options...
wildteen88 Posted July 25, 2009 Share Posted July 25, 2009 Okay so the short answer is you can't. Not without passing them in the url as before. You can make the buttons into form submit buttons, with hidden fields passing, but that no longer really makes them links. Alternatively you could parse the new url for the info, since the new link is unique and contains the id, name, etc.. right? barney0o0 is using mod_rewrite. So not sure what you're suggesting Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882661 Share on other sites More sharing options...
barney0o0 Posted July 25, 2009 Author Share Posted July 25, 2009 ok, so this is where i feel thick... if the visible link is whatson/12/comedy/personsname (without any id=12 cat=comedy etc), how do i asign the values to the query (or this referencing the htaccess)? Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882664 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 Okay so the short answer is you can't. Not without passing them in the url as before. You can make the buttons into form submit buttons, with hidden fields passing, but that no longer really makes them links. Al ternatively you could parse the new url for the info, since the new link is unique and contains the id, name, etc.. right? barney0o0 is using mod_rewrite. So not sure what you're suggesting Right. He is using mod_rewrite, so that if a user were to go to www.somepage.com/somepage.php?id=1&name=Joe it would be rewritten to www.somepage.com/1-Joe/ well the problem is that now on his pages he wants to make his links be www.somepage.com/1-Joe/ so he's not actually passing params in the url anymore. Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882670 Share on other sites More sharing options...
wildteen88 Posted July 25, 2009 Share Posted July 25, 2009 That is down to your rewrite rule. Which is rewriterule ^whatson/([^-]+)-([^&]+)\.html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [L] The url to the right (highlighted) is what your PHP script see's. The parts in red will be your $_GET vars. As I mention earlier As you would normally would using the $_GET superglobal. The variables you'll need are $_GET['event_id'] $_GET['cat_name'] $_GET['event_name'] Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882672 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 Okay so maybe I'm being a nub here (haven't really used modrewrite all that much). So you are saying that when you have that rule in .htaccess, on an actual page you can do <a href='somepage.php/1-blah/'>go to blah page!</a> and because of the rewrite rule, I can use $_GET['event_id'], $_GET['event_name'], etc..? Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882676 Share on other sites More sharing options...
wildteen88 Posted July 25, 2009 Share Posted July 25, 2009 Okay so maybe I'm being a nub here (haven't really used modrewrite all that much). So you are saying that when you have that rule in .htaccess, on an actual page you can do <a href='somepage.php/1-blah/'>go to blah page!</a> and because of the rewrite rule, I can use $_GET['event_id'], $_GET['event_name'], etc..? Basically yes. Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882680 Share on other sites More sharing options...
barney0o0 Posted July 25, 2009 Author Share Posted July 25, 2009 sooooooooooo, hopefully this will be last bit. for the query, would i use something like $passedid = $_GET['event_id']; or do i need to refer the rewrite ($1) value? oh, and thanks very much for your help with this That is down to your rewrite rule. Which is rewriterule ^whatson/([^-]+)-([^&]+)\.html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [L] The url to the right (highlighted) is what your PHP script see's. The parts in red will be your $_GET vars. As I mention earlier As you would normally would using the $_GET superglobal. The variables you'll need are $_GET['event_id'] $_GET['cat_name'] $_GET['event_name'] Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882682 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 Holy crap I learned something new <3 barney: In the script itself, you would use $passedid = $_GET['event_id']; Though I suggest you sanitize it first... Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882684 Share on other sites More sharing options...
wildteen88 Posted July 25, 2009 Share Posted July 25, 2009 @barney0o0 Just to let you know your current rewriteRule will not work as you'd expect. it will only pass over the event id and category. You should this as the rewrite rule instead: rewriteRule ^whatson/([0-9+])-([A-Z0-9_]+)-([A-Z0-9_]+).html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882687 Share on other sites More sharing options...
barney0o0 Posted July 25, 2009 Author Share Posted July 25, 2009 cheers, il have a go in the morning (the heats got to me too much!) thanks again Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-882693 Share on other sites More sharing options...
barney0o0 Posted July 27, 2009 Author Share Posted July 27, 2009 ok, i had a 'play' yesterday....without any problems, but a couple of questions. as a update, links are as; <a href="whatson_detail/<?php echo $row_Recordset1['event_id']; ?>/<?php echo $row_Recordset1['cat_name']; ?>/<?php echo $row_Recordset1['event_name']; ?>.html" andf the htacces file is; <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriterule ^whatson_detail/([^-]+)/([^&]+)\.html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [L] </IfModule> (the event name $3 isnt important as passed variable, so i left it out) so, questions, questions.. If i want to add 2 more links (one to whatson.php and another to whatson.php?<?php echo $row_Recordset1['cat_name']; ?>)...how would i write these links and how woudl i update the htaccess file? i think once i understand these bits i should be ok...fingers crossed. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-883784 Share on other sites More sharing options...
wildteen88 Posted July 27, 2009 Share Posted July 27, 2009 ok, i had a 'play' yesterday....without any problems, but a couple of questions. as a update, links are as; <a href="whatson_detail/<?php echo $row_Recordset1['event_id']; ?>/<?php echo $row_Recordset1['cat_name']; ?>/<?php echo $row_Recordset1['event_name']; ?>.html" andf the htacces file is; <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriterule ^whatson_detail/([^-]+)/([^&]+)\.html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [L] </IfModule> (the event name $3 isnt important as passed variable, so i left it out) The reason why is because you only have two possible matches in your rewrite rule. This is why I suggested you to use the following instead rewriteRule ^whatson/([0-9+])/([A-Z0-9_]+)/([A-Z0-9_]+).html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [NC,L] In order for you to understand whats happing in your rewriterule you need to know the basic syntax for regex (regular expressions). Each possible match is wrapped in parenthesis (). Here is a basic visual representation Example URL: mysite.com/whatson/123/cat_name/event_name.html RewriteRule rewriteRule ^whatson/([0-9+])/([A-Z0-9_]+)/([A-Z0-9_]+).html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 so, questions, questions.. If i want to add 2 more links (one to whatson.php and another to whatson.php?<?php echo $row_Recordset1['cat_name']; ?>)...how would i write these links and how woudl i update the htaccess file? i think once i understand these bits i should be ok...fingers crossed. Thanks in advance You need to write separate rewriteRules for each type of url. So if you have the following possible urls mysite.com/whatson/123/cat_name/event_name.html mysite.com/whatson/123/cat_name.html mysite.com/whatson/123/ You'll have to have the following rules rewriteRule ^whatson/([0-9+])/([A-Z0-9_]+)/([A-Z0-9_]+).html$ /whatson.php?event_id=$1&cat_name=$2&event_name=$3 [NC,L] rewriteRule ^whatson/([0-9+])/([A-Z0-9_]+).html$ /whatson.php?event_id=$1&cat_name=$2[NC,L] rewriteRule ^whatson/([0-9+])/$ /whatson.php?event_id=$1[NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-884121 Share on other sites More sharing options...
barney0o0 Posted July 27, 2009 Author Share Posted July 27, 2009 lovely...thanks for your time in explain this to me....very well written and informative. I was a bit nervous about having numerous whatson rewrites (thinking that they conflict) With reference to the whatson link....would it just be whatson or whatson/ many thanks Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-884138 Share on other sites More sharing options...
barney0o0 Posted August 21, 2009 Author Share Posted August 21, 2009 sorry, i know its an old post, but ive come under a little problem... For example i have this link <?php $out2 = str_replace(" ", "-", $row_Recordset1['event_name']); ?><a href="whatson_detail/<?php echo $row_Recordset1['event_id']; ?>/<?php echo $row_Recordset1['cat_name']; ?>/<?php echo $out2?>.html"><?php echo $row_Recordset1['event_name']; ?></a> And ive changed the htaccess file to: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriteRule ^whatson_detail/([0-9+])/([A-Z0-9_]+)/([A-Z0-9_]+)\.html$ /whatson_detail.php?event_id=$1&cat_name=$2&event_name=$3 [NC,L] rewriteRule ^whatson.html$ /whatson.php [NC,L] </IfModule> ......however i get a page not found. Strangely enough, initially i didnt setup the htacces file correctly (forgot one variable) , however it worked using following htaccess and the inital link (above) <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriterule ^whatson_detail/([^-]+)/([^&]+)\.html$ /whatson_detail.php?event_id=$1&cat_name=$2&event_name=$3 [L] rewriteRule ^whatson.html$ /whatson.php [NC,L] </IfModule> ...... i hope that someone can give me a push in the right direction. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/167394-seo-friendly-link/#findComment-903301 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.