Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

htaccess static link containing ID in HTML page name to dynamic link 2

Status
Not open for further replies.

TheQuestioner

Programmer
Jun 29, 2002
78
GB
I'm having problems with htaccess trying to process a static link into a dynamic link, where the variable value is embedded within the URL HTML page name.

This is how a typical link should look:

Code:
[URL unfurl="true"]http://www.mywebsite.com/flux_capacitor_114.html[/URL]

Where [tt]flux_capacitor_[/tt] is the name of the widget (and can be called anything), and [tt]114[/tt] is the ID of the widget.

I'm only interested in the widget ID. I need to extract it and redirect to:

Code:
[URL unfurl="true"]http://www.mywebsite.com/product_detail.php?id=114[/URL]

So basically, I need to set up a rule which:-

[ol]
[li]Looks for a number within the HTML page name of the static URL[/li]
[li]Extract only the number and add it into the redirect dynamic PHP after the "id=" part[/li]
[/ol]

I've got as far as this:

Code:
#rule to interpret [URL unfurl="true"]http://www.mywebsite.com/product-114.html[/URL]
RewriteRule ^product-([0-9]+)\.html$ product_details.php?id=$1 [NC,L]

But if I change the [tt]product[/tt] to any other text (in the redirect rule as well as the source URL), it stops working.

Please help.
 
First, you must disallow product_details or you will get a loop.

Then, you must use use "keeps" to take the two variable parts (the product name and the number) but ignore the 1st (you don't need the product name) and insert the second as id=

Something like (untested):

RewriteEngine on
RewriteCond %{REQUEST_URI} !^product_detail\.php
RewriteRule ^(.*)_([0-9]+)\.html$ [NC,L]
 
Thank you both warpzero and elgrandeperro. Sorry for the delay, but I was working on other parts of my project and decided to revisit this issue at the end.

Unfortunately warpzero, the text at the beginning will change for each widget (as it will be the name of the widget itself).

elgrandeperro, with a few minor modifications I managed to get your code working fine:-

Code:
#RewriteCond %{REQUEST_URI} !^product_detail\.php
RewriteRule ^(.*)-([0-9]+)\.html$ product_details.php?id=$2 [NC,L]

However, I now also need to include a second variable into the URL so that it looks like this:

Code:
[URL unfurl="true"]http://www.mywebsite.com/flux_capacitor_114.html/3/[/URL]

Which eventually maps onto:-

Code:
[URL unfurl="true"]http://www.mywebsite.com/product_detail.php?id=114&picid=3[/URL]

Any suggestions would be greatly appreciated.
 
I've managed to get this nearly working, by using

Code:
RewriteRule ^(.*)-([0-9]+)\.html?/?([0-9a-zA-Z-]+)?/?$ [URL unfurl="true"]http://mywebsite/product_details.php?id=$2&picid=$3[/URL] [QSA,L]

The only problem is that the old "pretty" URL ( in the browser URL textbox is replaced with the redirected "ugly" URL (
Is there a way of keeping the old URL visible in the browser whilst redirecting to the new URL?
 
I believe if you don't use in the target rewrite, it will do a internal rewrite and the URL will appear correct. Like in your example (I believe it is mistyped (_ instead of - before the number):

RewriteRule ^(.*)_([0-9]+)\.html?/?([0-9a-zA-Z-]+)?/?$ [QSA,L]

were instead

RewriteRule ^(.*)-([0-9]+)\.html?/?([0-9a-zA-Z-]+)?/?$ /product_details.php?id=$2&picid=$3 [QSA,L]

it might work.
 
Thanks elgrandeperro, but unfortunately if I omit the full URL then the page is not displayed correctly. The relative URLs' that are used for each page seem to change and take on the URL that was passed in prior to the redirect.

For example if the URL is
Code:
[URL unfurl="true"]http://www.mywebsite.com/flux_capacitor_114.html[/URL]

Then the relative URL for the About Us page (which appears as a link on this page), should show

Code:
[URL unfurl="true"]http://www.mywebsite.com/about_us[/URL]

but instead shows

Code:
[URL unfurl="true"]http://www.mywebsite.com/flux_capacitor_114.html/about_us/[/URL]

Any ideas?
 
Dang, it only really works in the same directory. The only way to get it to work would be to use the R redirect, but that will screw up the rewriterule.

If the "about_us" URL could be like href="/about_us.html" instead of href="about_us.html", then it would work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top