designer76 Posted April 30, 2011 Share Posted April 30, 2011 I am having a little trouble using the Mod rewrite in .htaccess to rewrite my url when I click my paging links. I have included some sample rewrite attempts that I have failed to get working, and a small sample of what some of my paging code looks like. For the life of me, I can't get this to work correctly. Basically I want: http://www.domain.com/portfolio?page=1 To look like: http://www.domain.com/portfolio/1 My Rewrite Attempt Examples: RewriteRule ^/([0-9]+)$ /$targetpage?page=$1 RewriteRule ^portfolio/([0-9]+)/$ /targetpage?page=$1 [L] Small Snippet Of Paging Code Example: $top_pagination .= "<a class='search_pages' href='$targetpage?page=$counter'>$counter</a>"; } $top_pagination .= "<a class='search_pages' href='$targetpage?page=$lpm1'>$lpm1</a>"; $top_pagination .= "<a class='search_pages' href='$targetpage?page=$lastpage'>$lastpage</a>"; } else { $top_pagination .= "<a class='search_pages' href='$targetpage?page=1'>1</a>"; $top_pagination .= "<a class='search_pages' href='$targetpage?page=2'>2</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/ Share on other sites More sharing options...
designer76 Posted May 3, 2011 Author Share Posted May 3, 2011 Can anyone help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1209700 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 Mod rewrite is for rewriting urls, not for rewriting the html pages you send to people. If you want the url's of yoour pages to be in the rewritten format, you need to do that in your code. Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1209782 Share on other sites More sharing options...
designer76 Posted May 3, 2011 Author Share Posted May 3, 2011 Mod rewrite is for rewriting urls, not for rewriting the html pages you send to people. If you want the url's of yoour pages to be in the rewritten format, you need to do that in your code. I am trying to rewrite the actual URL in the web browser unless I am misunderstanding what you are saying. In my web browser, my paging links look like: http://www.domain.com/portfolio?page=1, but I want to use modrewrite to make them look like: http://www.domain.com/portfolio/1 or http://www.domain.com/portfolio-1. I have tried multiple methods, but I can't seem to get this to work. Is this possible? BTW, thanks for your reply! Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1209828 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 My answer is the same. Your paging links are what you are sending to the client in your html page. If you want them to be of the format http://www.domain.com/portfolio/1 then that is how they should be in the anchor tags you are creating in your script. Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210006 Share on other sites More sharing options...
designer76 Posted May 4, 2011 Author Share Posted May 4, 2011 O.K. gotcha, how would I go about changing the links in my actual code to make them look like how I want? I have tried a few methods, but it prevents my paging from working altogether. I found someone who was having the exact same issue I am, and his paging script looked similar to mine, but I have had trouble adapting my code to work like his did. Can you take a look his solution below and suggest how I can make this work with my example? Or is there a different method altogether that I should use to reconfigure my actual HTML links? Thanks! The solution he was given: Try Code: Select all Options +FollowSymLinks RewriteEngine On RewriteRule ^my-product-([0-9]+)-([0-9]+)\.htm$ /Product.php?ID=$1&page=$2 [QSA,L] RewriteRule ^my-product-([0-9]+)\.htm$ /Product.php?ID=$1 [QSA,L] And replacing the following lines (What his original paging links looked like, very similar to mine, minus the ID=$ID) echo " <a href='{$_SERVER['PHP_SELF']}?ID=$ID&page=1'><<</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?ID=$ID&page=$prevpage'><</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?ID=$ID&page=$x'>$x</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?ID=$ID&page=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?ID=$ID&page=$totalpages'>>></a> "; With echo ' <a href="/my-product-' . $ID . '-1"><<</a> '; echo ' <a href="/my-product-' . $ID . '-' . $prevpage . '"><</a> '; echo ' <a href="/my-product-' . $ID . '-' . $x . '">$x</a> '; echo ' <a href="/my-product-' . $ID . '-' . $nextpage . '">></a> '; echo ' <a href="/my-product-' . $ID . '-' . $totalpages . '">>></a> '; Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210182 Share on other sites More sharing options...
gizmola Posted May 4, 2011 Share Posted May 4, 2011 Have the links look the way they need to be so that if someone typed one in, it would work correctly. It seems like you want to make this more complicated than it is. Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210236 Share on other sites More sharing options...
designer76 Posted May 4, 2011 Author Share Posted May 4, 2011 I am really not trying to make it more complicated, I have tried what you are suggesting, but my problem is that my paging links will not work without my links physically looking like: $targetpage?page=1. My links will not work unless I have the question mark after the $target page and the page= in my URL. I have gone in to my HTML pages and tried making my links look like: http://www.domain.com/portfolio/page-1 or http://www.domain.com/portfolio-1, but the paging links do not work when written like that in the HTML. That is why I am not understanding what I am doing wrong or how to make this work. Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210300 Share on other sites More sharing options...
gizmola Posted May 4, 2011 Share Posted May 4, 2011 So in summary, it seems the problem here is that you don't understand how mod_rewrite works. I don't mean to be flippant, but this is a common question. In a nutshell, mod_rewrite is near impossible to understand if you don't understand the basics of regular expressions. It looks like you have cut and pasted someone elses rewrite rules but they don't correlate to your system. Let's go back to something you had earlier. Let's assume your script is named "thescript.php". It takes a single get parameter of "page=". You want your rewrite rule to work like this: yourdomain/portfolio/1 -> becomes yourdomain/thescript.php=1 You have been close to the right answer, but it looks like you have not been able to keep straight the difference between php and php variables (with the $ in the name) and things in mod_rewrite which have nothing to do with php and don't know anything about php variables, but that is just a guess. My best guess for you is: RewriteRule ^portfolio/([0-9]+) thescript.php?page=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210578 Share on other sites More sharing options...
designer76 Posted May 5, 2011 Author Share Posted May 5, 2011 MAN! I figured out what I was doing wrong. You are right in that I am new to mod_rewriting, so I had things written completely in reverse. I finally got things working with the help of your last post gizmola. My only last question is that when I initially click on the paging link, everything works fine. The problem that I am having is that when I click on another paging link, it is adding an extra portfolio/ in the url every time. So for example: I click on the page 2 link and my url reads: http://domain.com/portfolio/2 which is fine. The issue is that when I click on the page three link my url now reads: http://domain.com/portfolio/portfolio/3, then if I click back on the page 2 link it reads: http://domain.com/portfolio/portfolio/portfolio/2 and so on. Do you know what may be causing this/how to fix this issue? Thanks again for your help and patience! Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210891 Share on other sites More sharing options...
designer76 Posted May 5, 2011 Author Share Posted May 5, 2011 Actually, I figured it out. I need to have the full URL links in there. Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/235199-i-have-a-quick-question-on-url-re-writing/#findComment-1210912 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.