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

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

本文介绍了重写URL以隐藏真实页面路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在SO的帮助下解决了我的htaccess重写程序问题:

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重新写入的情况在地址栏中,例如:

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

怎么可能这适用于我的情况吗?我真的希望有一个很小的url,但似乎我错过了一些东西。

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]

这将起作用,只要替换路径中的两个 whatever都相同即可。如果不是,则必须对最后的任何内容进行硬编码,如下所示:

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以隐藏真实页面路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:05