URL rewriting-code for rewriting

前端 未结 2 1623
野性不改
野性不改 2020-11-30 15:34

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

2条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 16:17

    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]
    

提交回复
热议问题