.htaccess 重写url以隐藏实际页面路径
我最近在SO的帮助下解决了我的htaccess重写规则问题: 结果是当有人进来的时候.htaccess 重写url以隐藏实际页面路径,.htaccess,url-rewriting,.htaccess,Url Rewriting,我最近在SO的帮助下解决了我的htaccess重写规则问题: 结果是当有人进来的时候 whatever.example.com/anypage 在地址栏中,htaccess自动重定向到 whatever.example.com/somepath/whatever/anypage 我想做的是找到一种方法,在地址栏中显示whater.example.com/anypage,并显示whater.example.com/somepath/whater/anypage的内容 在我前面提到的帖子中,J
whatever.example.com/anypage
在地址栏中,htaccess自动重定向到
whatever.example.com/somepath/whatever/anypage
我想做的是找到一种方法,在地址栏中显示whater.example.com/anypage
,并显示whater.example.com/somepath/whater/anypage
的内容
在我前面提到的帖子中,Jon Lin明确提到了以下几点:
重定向始终更改浏览器URL地址栏中的内容
然而,我知道一些非常常见的url重写情况会出现在地址栏中,比如:
example.com/article-1-15
但实际上显示了
example.com/somepath/somepage.php?article=1&otherparam=15
这怎么可能适用于我的情况?我真的希望有一个小小的url,但似乎我错过了一些东西。您可以尝试以下内容:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://whatever.example.com/somepath/whatever/%1 [L]
RewriteRule .* http://%1.example.com/somepath/whatever/%2 [L]
它将映射:
http://whatever.example.com/anypage
发送到以下位置的资源:
http://whatever.example.com/somepath/whatever/anypage
始终在浏览器的地址栏中显示:
http://whatever.example.com/anypage
已更新
如果任何内容都是动态的,并且在替换URI中相同,那么下面是另一个选项:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ([^/]+)\.example\.com.*
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://%1.example.com/somepath/%1/%2 [L]
只要替换路径中的两个“whatever”相同,这将起作用。如果不是,则最后一个“whatever”必须硬编码,如下所示:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://whatever.example.com/somepath/whatever/%1 [L]
RewriteRule .* http://%1.example.com/somepath/whatever/%2 [L]
没有其他方法,因为传入的URL没有它。“无论什么”都是动态的,你确定你的htaccess会这样做吗?好的,确保它做你需要的事情的唯一方法是测试它。你难道看不到你的htaccess永远不会处理动态子域吗?也许我弄错了,但这似乎很明显,因为您正在重写规则中对其进行硬编码。规则试图做的是将最后一个文件夹
/anypage
保留为最后一个文件夹。仅此而已,所以文件夹或子域的名称并不重要。@Sebas我理解你在评论中所说的,所以我更新了我的答案。从你的问题来看,我不是这样看的。