views:

300

answers:

2

hi to all, i am working for a site,which is in php.....i want to rewrite url

e.g www.3idiots.co.in/stories.php?id=17

if i want to rewrite it as

www.3idiots.co.in/stories/17.html

can any one tell me the code for this to write in .htaccess file.?

+1  A: 

I'm assuming you're using Apache with mod_rewrite. Something like

RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1

should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.

Walter H
You forgot one "/" after "stories":RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
slosd
You're quite right, I've fixed it now.
Walter H
A: 

mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.

After that, you can use the rule suggested by José Basilio:

RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1

Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:

RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]
Gumbo