htaccess内部请求和外部请求区别
我的 .htaccess
文件有问题。我尝试过谷歌搜索,但找不到任何有用的东西。
我有一个 AJAX 请求将页面加载到 index.php 中。触发它的链接通过 jquery 在前面加上“#”。因此,如果您点击链接 domain.com/foo/bar (wordpress 永久链接),您将在浏览器中看到 domain.com/#/foo/bar 和内容将通过 AJAX 加载。
我的问题是:由于这些是博客文章,外部链接会获取真正的链接(domain.com/foo/bar),所以我希望它们重定向到domain.com/#/foo/bar(因为ajax会检查哈希值)并发挥其魔力)。
示例此处。
前置的 jquery 代码是:
$allLinks.each(function() {
$(this).attr('href', '#' + this.pathname);
...
然后脚本检查
if (hash) { //we know what we want, the url is not the home page!
hash = hash.substring(1);
URL = 'http://' + top.location.host + hash;
var $link = $('a[href="' + URL + '"]'), // find the link of the url
...
现在我试图让重定向与 htaccess 一起使用。我需要检查请求是外部的还是内部的
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1 #???
,以及 uri 是否以“/#/”开头,这是一个问题,因为它是一条注释,\%23 确实无法正常工作。
RewriteCond %{REQUEST_URI} !^/\%23/(.*)$ #???
我如何让它工作以简单地将外部请求从domain.com/foo/bar重定向到domain.com/#/foo/bar而不影响内部AJAX内容?
I have a problem with an .htaccess
file. I've tried googling but could not find anything helpful.
I have an AJAX request loading pages into the index.php. The link triggering it is getting prepended by "#" via jquery. So if you click on the link domain.com/foo/bar (a wordpress permalink) you get domain.com/#/foo/bar in the browser and the content will get loaded via AJAX.
My problem is: Since these are blog posts, external links grab the real link (domain.com/foo/bar), so I want them to get redirected to domain.com/#/foo/bar (cause then ajax checks the hash and does its magic).
Example here.
The jquery code for the prepend is:
$allLinks.each(function() {
$(this).attr('href', '#' + this.pathname);
...
and then the script checks
if (hash) { //we know what we want, the url is not the home page!
hash = hash.substring(1);
URL = 'http://' + top.location.host + hash;
var $link = $('a[href="' + URL + '"]'), // find the link of the url
...
Now I am trying to get the redirect to work with htaccess. I need to check if the request is external or internal
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1 #???
and if the uri starts with "/#/" which is a problem since it's a comment then, \%23 does not really work somehow.
RewriteCond %{REQUEST_URI} !^/\%23/(.*)$ #???
How do I get this to work to simply redirect an external request from domain.com/foo/bar to domain.com/#/foo/bar without affecting the internal AJAX stuff?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您的
$allinks
变量的分配方式与此类似:请执行以下操作:
这只会将内部链接转换为您的
hash
-y 样式。I suppose your
$allinks
variable is assigned in a fashion similar to this:Do this instead:
This will transform internal links to your
hash
-y style only.好的,我已经用 PHP 完成了,这里是代码
肯定有更好的解决方案,但这暂时可以解决问题,因为页面 /foo/bar 在我的情况下不包含 header.php ,所以没有 > ;body<-tag 和 php“header()”函数有效。如果有人知道 htaccess 脚本,我很想知道和学习。
Ok i've done it with PHP here is the code
There sure is a better solution, but this does the trick for now and since the page /foo/bar does, in my case, not include the header.php there is no >body<-tag and the php "header()" function works . If anyone knows the htaccess script for this I am keen to know and learn.