首页 \ 问答 \ 通过htaccess重定向,其中IP不是&&删除index.php(redirect via htaccess where IP is not && remove index.php)

通过htaccess重定向,其中IP不是&&删除index.php(redirect via htaccess where IP is not && remove index.php)

我通常使用这个htaccess文件从ExpressionEngine中的URL中删除index.php

AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

AcceptPathInfo On

Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On

Options +FollowSymLinks

# Looks for files and directories that do not exist
# and provide the segments to the index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^/index.php
RewriteCond $1 !.(css|js|png|jpe?g|gif|ico)$ [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>  

虽然这很有效,但在我们将此网站投入生产之前,我们通过此htaccess文件将所有流量指向给定的网址

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteRule ^(.*)$ http://www.anotherdomain.com/ [R=301,NC]

我自己的IP地址正在替换localhost调用,以便我可以访问该站点。

基本上我正在寻找的是这两个的组合,它将从我的URL中删除index.php,但仍然重定向其他人。

谢谢,史蒂文


I usually use this htaccess file to remove index.php from my URLs in ExpressionEngine

AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

AcceptPathInfo On

Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On

Options +FollowSymLinks

# Looks for files and directories that do not exist
# and provide the segments to the index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^/index.php
RewriteCond $1 !.(css|js|png|jpe?g|gif|ico)$ [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>  

While that works great, before we move this site into production, we're directing all traffic to the given url to another via this htaccess file

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteRule ^(.*)$ http://www.anotherdomain.com/ [R=301,NC]

My own ip address is replacing the localhost call so that I can access the site.

Basically what I'm looking for is a combination of these 2 that will remove index.php from my URLs for me but still redirect everyone else.

Thanks, Steven


原文:https://stackoverflow.com/questions/13439519
更新时间:2022-04-05 06:04

最满意答案

代码的主要问题在于:

        var subRow = myRow[z].lastChild; 

这为某些行提供了文本节点对象,而不是TD元素。 并且文本节点没有innerHTML属性。 由于其余的代码都适用undefined ,因此在计算中undefined ,并且出现问题。

但是,由于您已经有了在addProduct函数中计算总和的代码,因此在removeBtn事件处理程序中几乎复制该代码是很遗憾的。

所以,我建议创建一个单独的函数,它将计算总数并显示它。 它将充分利用您的两个代码片段,避免上述问题:

function displayTotal() {
    // Code taken from the last part of addProduct: 
    var toPay = document.getElementsByClassName("altogether");
    var suma = 0;
    for (var n=0; n<toPay.length;n++) {
        // This condition is inspired by the removeBtn click handler:  
        if (!toPay[n].parentElement.classList.contains("strike")) { 
            suma = suma + parseFloat(toPay[n].innerHTML);
        }
    }
    // Only put the total at the end, not in the loop:
    howMuch.innerHTML = parseFloat(suma);
}

虽然与您提到的问题无关,但我建议您在开始修改表之前进行任何验证检查。 检查空输入也是不够的。 您还应该检查输入是否为数字。

考虑到这一点,您的addProduct函数可能如下所示:

function addProduct() {
    // Before doing anything test the input is complete:
    if (quantityInput.value == "" || isNaN(quantityInput.value)) {
        alert("Please add quantity");
        return; // and exit!
    }
    if (priceInput.value == "" || isNaN(priceInput.value)) {
        alert("Please add price");
        return; // and exit!
    }

    var removeBtn = document.createElement("button");
    removeBtn.innerHTML="Skreśl przedmiot";

    var row = table.insertRow(1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    var cell5 = row.insertCell(4);

    cell5.classList.add("altogether");
    cell1.appendChild(removeBtn); 
    cell2.innerHTML = productInput.value
    cell3.innerHTML = parseFloat(quantityInput.value);
    cell4.innerHTML = parseFloat(priceInput.value);
    cell5.innerHTML = parseFloat(quantity.value)*parseFloat(price.value);

    removeBtn.addEventListener("click", function(event){
        this.parentElement.parentElement.classList.toggle("strike");
        displayTotal();
    });
    // Code moved to function 
    displayTotal();
}

所以你注意到displayTotal在两个地方被调用:添加产品之后,以及切换删除之后。 这样您就避免了代码重复。

这是更新的小提琴

还有什么可以改进的

如果您将购物车内容保存在变量(具有项目,价格和数量属性的对象的数组)中会更好。 这样您就不必读取HTML元素来找回这些数据,这不是很有效。

其次,您应该更好地为textContent分配值,而不是innerHTML 。 这样你就可以避免特殊字符的问题。 例如,如果您的产品名称中包含<字符,则会看到部分产品未显示。 这是textContent不具备的。

最后,如果在真实应用程序中使用它,则购物车必须由服务器管理。 这是因为网页及其上的数字可以通过客户端软件,浏览器插件等轻松修改。因此它们不可信任。 所有检查都必须由服务器应用程序再次完成,该应用程序应该还有一个具有正确产品价格的数据库等。


The main issue with the code is in this line:

        var subRow = myRow[z].lastChild; 

That gives a text node object for some rows, not a TD element. And text nodes don't have an innerHTML property. Since the rest of your code works with that, you get undefined in your calculations, and things go wrong.

But, since you already have code that calculates the sum in the addProduct function, it is a pity to almost duplicate that code in the removeBtn event handler.

So, I would suggest to create a separate function, which will calculate the total and display it. It will take the best of both code pieces you have for that, avoiding the problem described above:

function displayTotal() {
    // Code taken from the last part of addProduct: 
    var toPay = document.getElementsByClassName("altogether");
    var suma = 0;
    for (var n=0; n<toPay.length;n++) {
        // This condition is inspired by the removeBtn click handler:  
        if (!toPay[n].parentElement.classList.contains("strike")) { 
            suma = suma + parseFloat(toPay[n].innerHTML);
        }
    }
    // Only put the total at the end, not in the loop:
    howMuch.innerHTML = parseFloat(suma);
}

Although not related to the problem you mentioned, I would also suggest to do any validation checks before you start modifying the table. It is also not enough to check for empty input. You should also check that input is numerical.

Taking that into account, your addProduct function could look like this:

function addProduct() {
    // Before doing anything test the input is complete:
    if (quantityInput.value == "" || isNaN(quantityInput.value)) {
        alert("Please add quantity");
        return; // and exit!
    }
    if (priceInput.value == "" || isNaN(priceInput.value)) {
        alert("Please add price");
        return; // and exit!
    }

    var removeBtn = document.createElement("button");
    removeBtn.innerHTML="Skreśl przedmiot";

    var row = table.insertRow(1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    var cell5 = row.insertCell(4);

    cell5.classList.add("altogether");
    cell1.appendChild(removeBtn); 
    cell2.innerHTML = productInput.value
    cell3.innerHTML = parseFloat(quantityInput.value);
    cell4.innerHTML = parseFloat(priceInput.value);
    cell5.innerHTML = parseFloat(quantity.value)*parseFloat(price.value);

    removeBtn.addEventListener("click", function(event){
        this.parentElement.parentElement.classList.toggle("strike");
        displayTotal();
    });
    // Code moved to function 
    displayTotal();
}

So you notice that displayTotal is called at two places: after adding a product, and after toggling the removal. This way you have avoided code duplication.

Here is the updated fiddle.

What could be improved still

It would be better if you would keep the shopping cart contents in a variable (an array with objects that have item, price and quantity properties). That way you do not have to read the HTML elements to find back these data, which is not very efficient.

Secondly, you should better assign values to textContent and not to innerHTML. That way you will avoid problems with special characters. If for instance your product name has a < character in it, you'll see part of it does not get displayed. This you will not have with textContent.

Finally, if this were used in a real application, the shopping cart would have to be managed by the server. This is because a web page, and the numbers on it can easily be modified by the client software, browser add-ins, etc. So they cannot be trusted. All checks have to be done again by a server application, that should also have a database with the correct product prices, etc.

相关问答

更多
  • 代码的主要问题在于: var subRow = myRow[z].lastChild; 这为某些行提供了文本节点对象,而不是TD元素。 并且文本节点没有innerHTML属性。 由于其余的代码都适用undefined ,因此在计算中undefined ,并且出现问题。 但是,由于您已经有了在addProduct函数中计算总和的代码,因此在removeBtn事件处理程序中几乎复制该代码是很遗憾的。 所以,我建议创建一个单独的函数,它将计算总数并显示它。 它将充分利用您的两个代码片段,避免上 ...
  • 从购物车中删除商品时,您将取消设置错误的值: unset($_SESSION['cart'][$remove]); 应该是你需要的一切。 } else { $_SESSION['cart'][$remove]; } 这一小段代码就是你需要从上面放置unset()调用的地方。 就目前来看,这个声明什么都不做。 You're unsetting the wrong values when removing items from the cart: unset($_SESSIO ...
  • 您必须将属性line-height添加到“li”元素样式: li { float: left; padding: 5px; margin-left: 92px; width: 324px; text-align: left; border-bottom: 1px solid #666; margin-bottom: 10px; /* like the image are 30px height + 16px padding, the line-height must be 4 ...
  • jQuery中没有ignore()方法,所以它会在控制台中抛出错误。 因此,要么克隆tr并从克隆的对象中删除span ,然后获取文本或获取所有不包含span和获取文本的td。 $(document).ready(function() { $('.sweet-container').on('click', 'tr span', function(){ var removedname = $(this).closest('tr').clone().remove('span').text( ...
  • 实际上在javascript中,var x = y; 表示将y的引用分配给x。 所以为了赋值,你必须使用var x = y.slice(); actually in javascript, var x=y; means assign reference of y to x. so in order to assign value you have to use var x=y.slice();
  • 只需将输入对放入某个类的div中(此处为“pair”) var input='