URL rewriting-code for rewriting

前端 未结 2 1595
野性不改
野性不改 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]
    
    0 讨论(0)
  • 2020-11-30 16:29

    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.

    0 讨论(0)
提交回复
热议问题