htaccess在jquery加载时递归加载

时间:2015-04-20 10:06:25

标签: php jquery .htaccess mod-rewrite

我有两个网址:

http://www.domain.com/catalogo
http://www.domain.com/catalogo/item1

我的.htaccess标题目前看起来像这样:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -Indexes -Multiviews
    RewriteEngine On

    RewriteBase /

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f 

    RewriteRule ^inicio$ index.php [L]
    RewriteRule ^catalogo$ catalogo.php [L]
    RewriteRule ^catalogo/(.+) catalogo.php/$1 [L]
    RewriteRule ^empresa$ empresa.php [L]
    RewriteRule ^contacto$ contacto.php [L]
</IfModule> 

我以这种方式将内容加载到catalogo.php

$("#contenido_catalogo").load("ajax_catalogo.php", {slu: 'lets_say_item1'}, function(){});

第一个网址已成功加载,但在网址上附加/item1会导致catalogo.php递归加载而不是ajax_catalogo.php

似乎ajax_catalogo.php的加载与第二个 RewriteRule 匹配并创建无限循环,但我不知道如何避免它。

我应该怎么做呢?谢谢。

1 个答案:

答案 0 :(得分:0)

首先,在每条规则之后添加[L]标志对您来说绝对有用 然后,定义RewriteBase或在规则中使用绝对路径 最后,在您的情况下,禁用MultiViews选项也是一个好主意。

您可以将此代码放入htaccess(必须在根文件夹中

Options -MultiViews

RewriteEngine On
RewriteBase /

RewriteRule ^catalogo$ catalogo.php [L]
RewriteRule ^catalogo/(.+)$ catalogo.php/$1 [L]

注意:在这种情况下,无需担心2条规则的顺序(它们不会干扰)


编辑:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -Indexes -Multiviews

    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule ^ - [L]

    RewriteRule ^inicio$ index.php [L]
    RewriteRule ^catalogo$ catalogo.php [L]
    RewriteRule ^catalogo/(.+)$ catalogo.php/$1 [L]
    RewriteRule ^empresa$ empresa.php [L]
    RewriteRule ^contacto$ contacto.php [L]
</IfModule>