简体   繁体   English

mod_rewrite编码后的URL?

[英]mod_rewrite to encode the url after the?

I want to tranform all the content of the URL after the ? 我想在?之后转换URL的所有内容。 to be encoded 被编码

example if I write this URL in the browser: 例如,如果我在浏览器中写这个URL:

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 这是因为Google将上述页面索引为

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

which does not exist 不存在

This where I have gotten to, I can replace / with % BUT not %2F because RewriteRule reads %2 as a regex group. 在这里,我可以用%BUT而不是%2F代替/,因为RewriteRule将%2读取为正则表达式组。 And then even if I can replace / with %2F modrewrite will probably read it as a / then creating an infinite loop. 然后,即使我可以将%2F替换为/,modrewrite也可能会将其读取为/,然后创建一个无限循环。

In my testing with the code I have so far 到目前为止,在对代码的测试中

localhost:81/?page=exams/product/vmware/ 本地主机:81 /?page = exams / product / vmware /

becomes 变成

localhost:81/?page=exams%25product%25vmware%25 本地主机: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 我不知道如何(或是否可以这样做)使用modrewrite但这里是javascript客户端的方式

<script>
    var p = window.location.pathname;
    var q = window.location.search;

    history.pushState('data', 'title', p+"/?"+encodeURIComponent(q.substr(1)));
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM