Forum Moderators: phranque

Message Too Old, No Replies

Help with .htaccess code for dynamic to static url

htaccess dynamic to static url help

         

SystemLord

4:35 am on May 13, 2008 (gmt 0)

10+ Year Member



Hi guys and girls
Well i have been trying to figure this out for over 3 days now, and nothing i have come across works.

I have a current .htaccess file that contains the code below


Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
ErrorDocument 404 http://www.example.com/404_error.html

I have been trying to get this dynamic URL;
http://www.example.com/headProduct.php3?part_num=AC165C2
to this static URL
http://www.example.com/headProduct/AC165C2.html

Now i have been trying this code:


Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc,L]
rewriterule headProduct-part_num-(.*)\.htm$ headProduct.php3?part_num=$1 [L]
ErrorDocument 404 http://www.example.com/404_error.html

In order to use the static URL like this:
http://www.example.com/headProduct-part_num-AC165C2.htm

But it's just not working, i have read though the apache.org rewriterule document:
[httpd.apache.org...]
And read though countless online tutorials that help with this topic. However i just cant get it to work.
I'd appreciate any help from you guys

Thank You
Manu

[edited by: SystemLord at 4:45 am (utc) on May 13, 2008]

[edited by: jdMorgan at 2:34 pm (utc) on May 13, 2008]
[edit reason] Please use example.com [/edit]

jdMorgan

2:39 pm on May 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The pattern in your rule does not match your example "static" URL:

For URL www.example.com/headProduct/AC165C2.html, the rule and pattern would be more like:


RewriteRule ^headProduct/([^/.]+)\.htm$ headProduct.php3?part_num=$1 [L]

Also, be sure to completely-flush your browser cache before testing any change to your code.

Jim

SystemLord

4:44 pm on May 13, 2008 (gmt 0)

10+ Year Member



If i use the code you gave me
RewriteRule ^headProduct/([^/.]+)\.htm$ headProduct.php3?part_num=$1 [L]
and try to use this URL
www.example.com/headProduct/AC165C2.htm
It doesnt work, i get a 404 error, however i dont get the custom 404 error i get the standard host 404 error.
I just dont get why this is not working...

Could someone one give me a code that should work 100%. Could this be a server side problem?

Thx for the reply jdMorgan

g1smd

6:22 pm on May 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** ErrorDocument 404 http://www.example.com/404_error.html

That code will give a 302 response as shown in the notes for Apache.

You must NOT include the domain name.

Only include the internal folder and file path.

SystemLord

6:32 pm on May 13, 2008 (gmt 0)

10+ Year Member



Alright, thx that one is fixed.

But the dynamic to static i still cant figure out would you mind helping me out with that one...

Thx

jdMorgan

6:47 pm on May 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the relevant contents of your server error log.

Jim

SystemLord

9:48 pm on May 13, 2008 (gmt 0)

10+ Year Member



all i can find is a traffic log, and its a mess to read. Is there an easy way to debug .htaccess

SystemLord

6:12 am on May 14, 2008 (gmt 0)

10+ Year Member



Ok got my dynamic to static redirect working, i do have a problem though, when i do a sitemap i still get the dynamic URL displayed. AKA with the ? As far as i know search engines do not index dynamic pages, that’s the whole reason i when though the trouble, but now that the static url does work and gets redirected to the dynamic one, will it not still look like a dynamic URL to the search engines?

Did i do it wrong?

RewriteRule ^cylinder_head-([^-]+)-([^-]+)-liter-([^-]+)-valve-([^-]+)-([^-]+)-side-([^-]+)-([^-]+)-([^/\.]+).html$ /headProduct.php?part_num=$8 [L]

Can someone explain this to me?
Thank You

g1smd

8:19 am on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** the static url does work and gets redirected to the dynamic one, will it not still look like a dynamic URL to the search engines? ***

That is NOT a redirect. It is a rewrite. It connects a "static" URL request to a "dynamic" internal filepath.

You do need a redirect. The redirect will redirect a request for a dynamic URL over to the static URL format. It must be a 301 redirect.

That is, one of the rules will contain [L] and the other will contain [R=301,L]. Be very clear on the differences between a redirect and rewrite.

[edited by: jdMorgan at 11:59 am (utc) on May 14, 2008]
[edit reason] speling [/edit]

SystemLord

3:32 pm on May 14, 2008 (gmt 0)

10+ Year Member



Aha, well i see my error then....

Ok so what do i do then? if i want this URL:

^cylinder_head-([^-]+)-([^-]+)-liter-([^-]+)-valve-([^-]+)-([^-]+)-side-([^-]+)-([^-]+)-([^/\.]+).html

to show in the URL bar of the user, but actually load this page:

/headProduct.php?part_num=$8

i am right where i started out :-( i guess i cant just replace the [L] with [R=301,L]

g1smd

9:00 pm on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The URLs on the other pages, in the links that people click on, define the URLs that users see and that search engines will access.

The rewrite connects those external URL requests to the internal filepath. That rule ends with [L], and the "right hand side" should contain just a path and filename. You've done that bit already. The rule must not contain the domain name; it's an internal rewrite.

If someone were to try to directly access the "dynamic" URL, they must be redirected [R=301] to the static format. This rule will have the domain name on the "right hand side". Failure to have this rule will mean that you will have a Duplicate Content problem on your hands.

So, there are three components to this, and so far you have only done one of them.

So, there are two rules, a rewrite and a redirect.

[edited by: g1smd at 9:48 pm (utc) on May 14, 2008]

SystemLord

9:33 pm on May 14, 2008 (gmt 0)

10+ Year Member



..ok, so i need to create a rewrite and a redirect and use the code that i already have. I am just lost because i cant find any working examples online, and i have tried everything the last couple of days. Either it doesnt work or the URL changes back to the static one.
Even if i try examples from SEO sites and tools.

I would appreciate if you could give a example of how to write it, then i would try it my self if i can figure it out. However at this point i am totally lost on what to do next..

Thx g1smd for your continued help though

jdMorgan

10:05 pm on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See if this helps: Changing dynamic to static URLs [webmasterworld.com] Search engine-friendly links with mod_rewrite

Jim

SystemLord

12:18 am on May 15, 2008 (gmt 0)

10+ Year Member



Wow i think i am just to stupid to figure it out....

Is this thinking process correct?
So php outputs a static URL that gets converted by htaccess to a dynamic URL. Which loads a page and then the URL gets R=301 redirected to a static URL that the search engine finally sees?

I have read and done the example used that the guide above gives.

Here is the code i tried but it dosnt rewrite the dynamic url to the static form:

RewriteRule ^cylinder_head-([^-]+)-([^-]+)-liter-([^-]+)-valve-([^-]+)-([^-]+)-side-([^-]+)-([^-]+)-([^/\.]+).html$ /headProduct.php?part_num=$8&s1=$1&s2=$2&s3=$3&s4=$4&s5=$5&s6=$6&s7=$7 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /headProduct\.php3\?part_num=([^&]+)&s1=([^&]+)&s2=([^&]+)&s3=([^&]+)&s4=([^&]+)&s5=([^&]+)&s6=([^&]+)&s7=([^&]+)\ HTTP/
RewriteRule ^headProduct\.html$ http://www.example.com/cylinder_head-%2-%3-liter-%4-valve-%5-%6-side-%7-%8-%1.html? [R=301,L]

Is there a mistake in the code?

Thx, I appreciate the help but I am going nuts i just dont get it

[edited by: jdMorgan at 1:24 am (utc) on May 15, 2008]
[edit reason] example.com [/edit]

g1smd

12:42 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** Here is the code i tried but it dosn't rewrite the dynamic url to the static form: ***

1. Are you sure about that? Going from dynamic to static should be a redirect not a rewrite.

2. The rewrite does not make any URLs *appear*. It merely translates a URL request from a web browser into an internal filepath and then gets that file without showing where in the internal filesystem that file came from.

The static URLs should be in the clickable links on your pages. The rewrite does NOT make those links. YOU make them by typing the URL into the bare HTML page and then FTPing that page on to your server.

*** So php outputs a static URL that gets converted by htaccess to a dynamic URL. Which loads a page and then the URL gets R=301 redirected to a static URL that the search engine finally sees? ***

No.

Your PHP script writes a URL to the page. A visitor can then click on that link to visit some other page. The link will "look static".

When someone clicks the link, their browser will request a URL from your site.

Your webserver translates the external (static) URL request into an internal (dynamic) filepath request to get the content. That translation is the rewrite, and is the line that ends in [L].

Additionally, and completely separately, you need another rule to stop people accessing the content directly through the dynamic URL. That rule takes the external (dynamic) request and sends back a 301 status code, and details of the URL that does now need to be requested instead. That rule is the redirect. It's the one that ends in [R=301,L].

I ask for "abc" and the server goes away and gets the content from "xyz", I stll see "abc" in my browser URL bar. I see "abc" in the links I clicked on. That's the URL that will be indexed.

I come back later and request "xyz". The server says 'nothing here, go fetch it from "abc" instead'. That's the redirect. search engines will not index "xyz". They will index the content under URL "abc" instead.

[edited by: g1smd at 12:53 am (utc) on May 15, 2008]

jdMorgan

12:48 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The user clicks on a link (somewhere) and makes a request to your server.

Your PHP script outputs a page containing one or more static URLs as links, which the browser then renders.

The user clicks on a static link and sends a request to your server.

A rule in htaccess internally rewrites that static URL to a dynamic filepath -- a call to your PHP script.

Your PHP script generates a new page, and sends that to the browser, which renders it.

Now a search engine comes along with an old dynamic URL that it has cached or has found elsewhere on the Web.

A rule in .htaccess detects that dynamic URL, and generates a 301-Moved Permanently external redirect to the equivalent static URL, telling the search engine to update its index, changing the URL for the requested resource from dynamic to static.

Using THE_REQUEST in the dynamic-to-static external redirect rule prevents it from trying to redirect the output of yout static-to-dynamic internal rewrite rule, thus avoiding an 'infinite' loop -- THE_REQUEST is always the actual request header received from the browser, regardless of any internal rewrites.

Change the "html" in the pattern of your second RewriteRule to "php3", change the very last subpattern of the RewriteCond from "[^&]+" to "[^\ ]+", and it should work better.

Jim

SystemLord

12:59 am on May 15, 2008 (gmt 0)

10+ Year Member



Alright, here is the action:
If i enter the URL below and i hit enter

[SITENAME.com...]

then it does the correct thing and loads the correct dynamic site, but it changes the URL to the dynamic url form below as well.

[SITENAME.com...]

I want it to load the dynamic URL but i want it to display the first static URL so that search engines can read the URL and index. So that they don’t discard the ? mark part of the dynamic URL’s.

So I guess I am missing two steps where the dynamic URL gets loaded internally on the server side but the appearing URL displays the static one.

**TEST1,TEST2,etc - Are variables that relate to the cylinder head, aka make liter ....**

[edited by: SystemLord at 1:03 am (utc) on May 15, 2008]

jdMorgan

1:11 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should install the "Live HTTP Headers" add-on for Firefox/Mozilla browsers and test with that. Assuming you corrected the dynamic-to-static redirect rule as outlined above, neither of your rules will cause the browser to show the dynamic URL in its address bar. So, either you've got other Redirect or RewriteRule directives on the server that are interfering, or your PHP script is generating a redirect -- We can't tell from here, but the headers checker will show it.

Jim

SystemLord

2:00 am on May 15, 2008 (gmt 0)

10+ Year Member



So you saying that my RewriteRule should NOT change the static url back to the dynamic url in the address bar, but rather should do it internally. Could this also be a host server problem?
Dont know if i can say this but i am using 1&1.

[edited by: SystemLord at 2:03 am (utc) on May 15, 2008]

SystemLord

2:07 am on May 15, 2008 (gmt 0)

10+ Year Member



alright installed, what part of the created log should i post? Everything once i click on the static link icon? (which still gets re-written to the dynamic one)

g1smd

7:35 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** So you saying that my RewriteRule should NOT change the static url back to the dynamic url in the address bar, but rather should do it internally ***

One rule (the rewrite) should cause the server fetch the dynamic content when you present the static URL. The visible URL should not change. That's the rule that should only have [L] at the end. The rule should also NOT mention the domain name. It should only specify the internal filepath.

SystemLord

9:53 pm on May 15, 2008 (gmt 0)

10+ Year Member



Yes, but isn’t that exactly what this rule is?
"
RewriteRule ^cylinder_head-([^-]+)-([^-]+)-liter-([^-]+)-valve-([^-]+)-([^-]+)-side-([^-]+)-([^-]+)-([^/\.]+).html$ /headProduct.php?
"
but it still gets re-written to the dynamic part after i click the link that uses the static form.

jdMorgan

11:51 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SystemLord,

Please pay attention to the distinction between an internal rewrite and an external redirect. Mixing up this terminology will lead you to an endless circle of frustration, and we've spent a good deal of time trying to describe this here already -- because it is critical to understand this point. A re-reading of the previous posts would be well-worth your time.

The code posted above, with the corrections suggested in later posts, WILL NOT cause an external redirect that changes the address bar of your browser. A redirect is the only way that the address bar can be updated from the server side, and the only redirect in that code is the one TO the static URL.

This code works as-is on tens of thousands of other servers (at least), some hundred of which are ones I control or have controlled. There is no question of the concept being correct, only the specific implementation or the server configuration is at question.

Therefore, as stated previously, you've got some other code in your server config or .htaccess files or in a script that is forcing a redirect AFTER the internal rewrite to the dynamic filepath has been completed.

The only thing that comes to mind to check for is this: During the redirect, is the domain always correct as far as "www" or non-www goes, or does it switch back and forth? It's possible that your server is configured with UseCanonicalName On --a relatively common problem-- and is forcing a redirect to what you would consider the "wrong" version of your domain. This redirect would 'expose' the internal script filepath as a URL. Your host will have to fix that, unless you have access to httpd.conf.

Jim

SystemLord

3:56 pm on May 16, 2008 (gmt 0)

10+ Year Member



Alright guys thx for the help, i finally got it to work. Code was incorrect, there is a "\" before the .html$ then it works.

RewriteRule ^cylinder_head-([^-]+)-([^-]+)-liter-([^-]+)-valve-([^-]+)-([^-]+)-side-([^-]+)-([^-]+)-([^/\.]+)\.html$ /headProduct.php3?part_num=$8 [L]

Admin can now mark this thread as "complete"

Thx for the help guys!

g1smd

6:06 pm on May 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I spent a whole evening last week looking at a server that gave "Error 500" every time.

I finally spotted the one rule with an extra space before the $, after hours of changing things around.

This stuff is completely unforgiving of typos.