Jump to content

I Have A Quick Question On URL Re-Writing!


designer76

Recommended Posts

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>";

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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> ';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.