重写 url 以隐藏真实页面路径 [英] rewriting url to hide real page path
问题描述
我最近在 SO 的帮助下解决了我的 htaccess rewriterule 问题:
I recently with the help of SO solved my htaccess rewriterule issue:
结果是有人进来时
whatever.example.com/anypage
在地址栏中,htaccess 自动重定向到
in the adress bar, the htaccess automatically redirects to
whatever.example.com/somepath/whatever/anypage
我想要做的是找到一种方法,在地址栏中仅显示 whatever.example.com/anypage
的内容为 whatever.example.com/somepath/whatever/anypage
显示.
What I wish to do is to find a way to just show whatever.example.com/anypage
in the adress bar with the content of whatever.example.com/somepath/whatever/anypage
displayed.
在我之前提到的帖子中,Jon Lin 清楚地提到了以下内容:
In the post I mentioned earlier, Jon Lin clearly mentions the following:
重定向总是会改变浏览器 URL 地址栏中的内容
redirects always change what's in the browser's URL address bar
但是我知道一些非常频繁的 url 重写案例会显示在地址栏中,例如:
However I know some very frequent cases of url rewritting that would show in the adress bar let's say, for instance:
example.com/article-1-15
但实际上显示了
example.com/somepath/somepage.php?article=1&otherparam=15
这如何适用于我的案例?我真的希望有一个小网址,但似乎我错过了一些东西.
How could this apply to my case? I really wish to have a tiny url but it seems I missed something.
推荐答案
你可以试试这样的:
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.
这篇关于重写 url 以隐藏真实页面路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!