Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite

         

Dragosh

7:35 am on Aug 30, 2007 (gmt 0)

10+ Year Member



This is the code in the .htaccess file

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^(.*)/(.*)/$ http://localhost/new/index.php?pagenum=$1&category=$2 [L]


It looks ok. But it should transform,for example [localhost...] to [localhost...] as far as i know,but it doesnt. but when i go directly to [localhost...] it returns in the browser adress bar [localhost...] .Is there any mistake in my .htaccess?

[edited by: Dragosh at 7:36 am (utc) on Aug. 30, 2007]

jdMorgan

1:32 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like a simple syntax problem: Do not include the protocol and domain unless you want to generate an external redirect:

RewriteRule ^([^/]+)/([^/]+)/?$ /new/index.php?pagenum=$1&category=$2 [L]

(I also made your patterns more efficient, and allowed the trailing slash to be missing.)

But it's possible that this description is the real problem:

But it should transform, for example http://localhost/new/index.php?pagenum=1&category=all to http://localhost/new/1/all

This rule translates the URL http://localhost/1/all, when it is requested from your server, to the filepath /new/index.php?pagenum=1&category=all

Mod_rewrite works on "incoming" URLs received by the server in the HTTP requests made by the client. It does not change URLs at all -- all it changes is the server filepath associated with a requested URL.

So if it is your intent to use mod rewrite to 'edit' the links on your pages, I'm afraid that won't work at all. It is the page which defines the URL, not the server. Mod_rewrite can only change the filepath to be used to serve the content for a requested page.

So, making 'friendly URLs' is a three-step process:
1) Change your links by editing the pages (or the script that produces your pages), to link to friendly URLs.
2) Add mod_rewrite code, so that the server can translate those friendly URLs, when requested, to the correct filepath for the requested page, or to invoke your script to generate the requested page.
3) (Optional) Add a mod_rewrite rule so that any direct client requests for the 'unfriendly' URL are externally redirected to the 'friendly' URL.

More info here [webmasterworld.com].

Jim