Rewrite url to rewrite

I work for a site that is in php ..... I want to rewrite url

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

if I want to rewrite it as

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

can someone tell me the code for this to write to the .htaccess. file?

+1
source share
2 answers

I assume you are using Apache with mod_rewrite. Sort of

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

gotta do the trick. Of course, you need to make sure that the RewriteRule is allowed in this directory. See this viking page for more details.

+3
source

mod_rewrite can only rewrite / redirect the requested URIs, not the ones contained in your HTML documents. Therefore, first make sure that your PHP application prints the correct URIs, therefore /stories/17.html instead of /stories.php?id=17 .

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

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

Although redirecting /stories.php?id=17 requests from outside to /stories/17.html and then internally back to /stories.php?id=17 possible, it’s not /stories.php?id=17 practice, as this will double the number of requests. But this is the rule for this:

 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
source

Source: https://habr.com/ru/post/895228/


All Articles