Forum Moderators: phranque

Message Too Old, No Replies

Apache is not Rewriting URLs on fly

Apache is not Rewriting URLs on fly

         

phparion

11:12 am on Mar 12, 2006 (gmt 0)

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



hi guys,

i have a simple site that generates URLs like

Code:
[localhost...]
[localhost...]
.. so on.

i want to rewrite the above URLs on fly to something like

Code:
[localhost...]
[localhost...]

respectively,

I wrote .htaccess file with code,

Code:
RewriteEngine on
RewriteBase /url
RewriteRule ^productid([^.]+).*$ products.php?id=$1 [T=application/x-httpd-php]

my target is that when i browse site it should change the dynamic URLs to static on fly and rename them with .htm extension. Well, now the problem is that its not working, i mean nothing happens when i browse site, and it still shows me the dynamic URLs generated from database. i am confused whether my Apache is not working or this .htaccess code is invalid.

please help me to accompish this task

thanks in advance

jdMorgan

5:27 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because the method required is opposite that which you expect.

To use 'friendly' URLs, do the following:

  • Change all dynamic links on all your pages to static links.
  • When a static link is requested from your server, internally rewrite it back to the dynamic form needed to call your script.
  • After this is done, externally redirect client requests for dynamic links to static links (at least until the search engines drop the dynamic links. Note that a special technique using the %{THE_REQUEST} server variable is needed to avoid an 'infinite' loop when doing this.)

    mod_rewrite takes action during the URL-to-filename translation phase of HTTP request processing, before any content-handlers are invoked and before any scripts are executed. It does not act on the content output by your server. Therefore, it cannot 'change the links on your pages' -- the ones that users click on and that search engines index. It can only change the file or script associated with a URL that is requested from your server.

    Always remember that this is incoming URL-rewriting, not output content-filtering.

    mod_rewrite will change an incoming requested URL matching the pattern on the left immediately following the RewriteRule directive to that given in the substitution URL on the far right side of the RewriteRule directive. So, a URL that matches the left-side pattern is rewritten or redirected to the filepath or URL specified on the right. Schematically:

    RewriteRule old_URL_pattern -> new_substitution_filepath/URL [flags]

    To make the job of changing your dynamic on-page URLs easier on a database-driven site, you can often make use of PHP preg_replace to change the links on-the-fly. So PHP is used to make your dynamic links static, people bookmark those static links and search engines list them in search results. Then when people or search engines request a static link from your site, mod_rewrite sends the request to your script in the dynamic form that it expects, the script generates a new page with static links on it, and the process starts over again.

    For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

    Jim

  •