Forum Moderators: phranque

Message Too Old, No Replies

"Redirect" working but not nice URL's

         

jackalandwolf

9:44 am on May 30, 2008 (gmt 0)

10+ Year Member



Hi guys

I can't seem to get my URL's rewritten without the query strings.

Here is my .htaccess file

I'm keeping it simple till I get it working.

RewriteEngine on
RewriteRule ^/?messages/([0-9]+)/?$ messages.php?records=$1 [L]
RewriteRule ^/?links\.php$ news.php [L]

The second rewrite rule is working. [If you test it] by going to
www.example.com/links.php, you'll see links.php in the url but the content of news.php

[Now, if you enter] www.example.com/messages.php?records=0,
[the URL isn't being] rewritten to
http://www.example.com/messages/0

I just can't seem to crack this one.
Please help.

[edited by: jdMorgan at 4:11 pm (utc) on May 30, 2008]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

4:38 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid you may have the whole concept backwards. mod_rewrite acts as an HTTP request is received from a client (browser or robot), and either generates a redirect or simply rewrites ("maps") the requested URL to a different internal filepath than what would be used if the mod_rewrite code was not present.

As such, your code says, "If we get a request for "/messages/<number>/", then pass the request to "messages.php" and put <number> in the query string as the value for the "records" name. So, your code rewrites the URL /messages/<number>/ to the filepath /messages.php/ with a query string of records=<number>. It does not do the reverse.

This is normally what is desired. You put links on your pages to search-engine-friendly "/messages/number/" URLs, and the server generates content for those URLs by using your messages.php script. After (and only after) these friendly URLs are all set up, then a third step can be used to generate an external redirect to remove the old unfriendly URLs like "/messages.php?record=<number>" from search engine listings.

This step, if not properly implemented, can lead to an infinite rewrite/redirect loop. In order to avoid a loop, the code must be sure that the unfriendly URL is being directly requested by the client, and not as a result of your internal rewrite above. For your application, it would look something like this:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /messages\.php\?record=([0-9]+)\ HTTP/
RewriteRule ^/?messages\.php$ http://www.example.com/messages/%1/? [R=301,L]

THE_REQUEST is the original HTTP request, exactly as received from the client, and unchanged by any internal rewriting. It would look something like this:
GET /messages.php?record=2008 HTTP/1.1

So, your pre-existing rule internally maps the friendly URL to your script, and this new rule will redirect any direct client request for an unfriendly URL to the equivalent friendly URL, thus removing it from search engine listings (over time). However, all links on all of your pages must be changed to point to the friendly URLs for this to be successful.

I hope this is clear. One thing that helps is to establish a good, solid footing on how these HTTP transactions work. And the best place to start is when a user clicks on a link on one of your pages. That link defines the URL and begins the transaction. All else is a result of that event. This knowledge is helpful, because things get very difficult to understand if you are trying to begin analyzing the transactions somewhere out in the middle of the process.

More info here [webmasterworld.com].

Jim

[edit] Code correction: Added "?" to the end of RewriteRule substitution URL to clear the query string. [/edit]

[edited by: jdMorgan at 2:39 pm (utc) on May 31, 2008]

jackalandwolf

12:03 pm on May 31, 2008 (gmt 0)

10+ Year Member



thanks! I'm going to follow your advice and study the modrewrite documentation.