Forum Moderators: phranque
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]
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]
Jim
Could someone one give me a code that should work 100%. Could this be a server side problem?
Thx for the reply jdMorgan
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
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]
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]
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]
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
Jim
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]
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]
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
[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]
Jim
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.
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
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!