Search code examples
apache.htaccessmod-rewriteurl-rewritingxampp

URL not re-writing as expected in apache XAMPP


The requested url is http://localhost/views/items?item=abc I want to rewrite it to http://localhost/views/myfile.php?reqtype=items&item=abc please note that the last keyword of url before query items I want to move to query section as reqtype=items to get this I wrote below code in .htaccess

RewriteEngine On
RewriteCond   %{QUERY_STRING}           ^(.*)$
RewriteRule   ^views/(.*)$          views/myfile.php?reqtype=$1&%1 [L,NE]

This code is working well when I tests here verified its output (from this link) by directly pasting in browser as URL worked well. but with xampp (my version is 7.1.28) it is not working. Remember url-rewriting is working for other type of conditions ( means apache configuration is correct and working ) but this particular example is not working, Any solution will be appreciated, Thanks.


Solution

  • Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs. NOTE that here condition RewriteCond %{ENV:REDIRECT_STATUS} ^$ is very important to stop looping else it will be keep redirecting. I have checked these rules by curl command and they work fine.

    RewriteEngine ON
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{THE_REQUEST} \s/views/(items)\?([^\s]*)\s [NC]
    RewriteRule ^(.*)$ views/myfile.php?reqtype=%1&%2 [NE,L]