htaccess内部请求和外部请求区别

发布于 2024-12-10 13:54:14 字数 1187 浏览 0 评论 0原文

我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅淡淡 2024-12-17 13:54:14

我想您的 $allinks 变量的分配方式与此类似:

$allinks = $('a');

请执行以下操作:

$allinks = $('a[href^="' + document.location.protocol + '//' + document.location.hostname + '"]');

这只会将内部链接转换为您的 hash-y 样式。

I suppose your $allinks variable is assigned in a fashion similar to this:

$allinks = $('a');

Do this instead:

$allinks = $('a[href^="' + document.location.protocol + '//' + document.location.hostname + '"]');

This will transform internal links to your hash-y style only.

治碍 2024-12-17 13:54:14

好的,我已经用 PHP 完成了,这里是代码

    $path = $_SERVER["REQUEST_URI"];
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
       echo "It's ajax";
    } else {
     if(strpos($path, '/#/') === false) {
       header("Location: http://schnellebuntebilder.de/#".$path); //ONLY WORKS IF THERE IS NO BODY TAG
     }
    }

肯定有更好的解决方案,但这暂时可以解决问题,因为页面 /foo/bar 在我的情况下不包含 header.php ,所以没有 > ;body<-tag 和 php“header()”函数有效。如果有人知道 htaccess 脚本,我很想知道和学习。

Ok i've done it with PHP here is the code

    $path = $_SERVER["REQUEST_URI"];
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
       echo "It's ajax";
    } else {
     if(strpos($path, '/#/') === false) {
       header("Location: http://schnellebuntebilder.de/#".$path); //ONLY WORKS IF THERE IS NO BODY TAG
     }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文