apachemod-rewriteurl-rewritingquery-stringquerystringparameter

mod_rewrite to encode the url after the?


I want to tranform all the content of the URL after the ? to be encoded

example if I write this URL in the browser:

http://www.mywebsite.com/?page=exams/product/vmware/

to be transformed to

https://www.mywebsite.com/?page=exams%2Fproduct%2Fvmware%2F

this is because Google index the above page as

http://www.mywebsite.com/product/vmware/

which does not exist


Solution

  • This where I have gotten to, I can replace / with % BUT not %2F because RewriteRule reads %2 as a regex group. And then even if I can replace / with %2F modrewrite will probably read it as a / then creating an infinite loop.

    In my testing with the code I have so far

    localhost:81/?page=exams/product/vmware/

    becomes

    localhost:81/?page=exams%25product%25vmware%25

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{QUERY_STRING} ^(.*)\/(.*)$
    RewriteRule ^(.*)$ $1?%1%%2 [N,E=redirect:true]
    RewriteCond %{ENV:redirect} ^true$
    RewriteRule (.*) $1 [R]
    </IfModule>
    

    I don't know how to (or if this can be done) with modrewrite BUT here is javascript client side way of doing this

    <script>
        var p = window.location.pathname;
        var q = window.location.search;
    
        history.pushState('data', 'title', p+"/?"+encodeURIComponent(q.substr(1)));
    </script>