且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重写 url 以隐藏真实页面路径

更新时间:2023-11-26 23:14:34

你可以试试这样的:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  ^/([^/]+)$
RewriteRule .*  http://whatever.example.com/somepath/whatever/%1 [L]

它会映射:

http://whatever.example.com/anypage

到一个资源:

http://whatever.example.com/somepath/whatever/anypage

始终显示在浏览器的地址栏中:

showing always in the browser's address bar:

http://whatever.example.com/anypage

更新

如果是动态的并且在替换 URI 中是相同的,这里是另一个选项:

If whatever is dynamic and is the same in the substitution URI, here is another option:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}  ([^/]+).example.com.*
RewriteCond %{REQUEST_URI}  ^/([^/]+)$
RewriteRule .*  http://%1.example.com/somepath/%1/%2 [L]

只要替换路径中的任何"都相同,这将起作用.如果不是,则必须对最后一个任何"进行硬编码,如下所示:

This will work as long as both "whatever" in the substitution path are the same. If they are not, the last "whatever" has to be hardcoded, like this:

RewriteRule .*  http://%1.example.com/somepath/whatever/%2 [L]

没有其他方法,因为传入的 URL 没有它.

There is no other way as the incoming URL doesn't have it.