Forum Moderators: phranque

Message Too Old, No Replies

problem of mod_rewrite

         

megame

11:44 am on Sep 14, 2004 (gmt 0)

10+ Year Member



Hi,everyone
I am new to Linux and Apache.My boss asked me to rewrite the url of my company's new database-driven website(PHP based). I have added the following content "Options +FollowSymLinks
RewriteEngine on" to the file "httpd.conf".I also setup the "mod_rewrite".The problem is that I can't get the search engine friendly url what I expected on my website.What should I do? Any tip will be appreciated.

Birdman

1:55 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a common misconception that you can use mod_rewrite alone to make your site's URLs SE friendly. The fact is, you have to modify the actual scripting of the site first, then use mod_rewrite to reform(internally) the URL into it's original state(querystring).

So, go into your script and find the block that outputs the ugly URLs. Modify it into the new look you want.(i.e. /widgets/yellow/6754.html)

Ok, now the SE sees a nice URL, but it doesn't exist. This is where mod_rewrite comes in.

RewriteRule ^widgets/(.*)/(.*)\.html$ /script.php?color=$1&id=$2 [L]

That will rewrite a request for
/widgets/yellow/6754.html
to
/script.php?color=yellow&id=6754

...where script.php is your main script. Of course, this is just an example and your parameters are probably different. But, you should get the idea.

Regards,
Birdman

PS: Welcome to WW!

megame

1:21 am on Sep 15, 2004 (gmt 0)

10+ Year Member



Thank you for your reply,Birdman.Yes, I have writen some rules in httpd.conf before,but the regular urls show up in address bar.The original url looks like this:www.mydomain.com/e_brand.php3?e_classcode=1,and I want it to be:www.mydomain.com/e_brand/1.html,the rewrite rule just like the following:RewriteRule ^/e_brand/(.*)\.html$ /e_brand.php3?e_classcode=$1 [L].Is there anything wrong? Or I need any additional .htaccess file to get the result?Thanks once again.

jdMorgan

2:45 am on Sep 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rule you posted is a rewrite, not a redirect. Therefore, the original URL will show in the address bar.

You should change the code to make it a redirect like this


RewriteRule ^/e_brand/([^.]+)\.html$ [b]http://www.yourdomain.com[/b]/e_brand.php3?e_classcode=$1 [[b]R=301,[/b]L]

if you wish to update the browser address bar and search engine listings.

See the references cited in our forum charter [webmasterworld.com] for more information.

Jim

megame

9:05 am on Sep 15, 2004 (gmt 0)

10+ Year Member



Thank you for your tips ,jdMorgan.