如何停止 .htaccess 循环 [英] How to stop .htaccess loop

查看:18
本文介绍了如何停止 .htaccess 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 htaccess 文件方面的专家,我正在尝试一些看起来很简单但我没能做到的东西.

我搜索并最终在这里找到了一些可以供我使用的东西,但它没有.代码如下.

这是我需要的示例:

http://localhost/Test/TestScript.php 显示这个:http://localhost/Test/TestScript/ 在原始位置使用脚本.>

这些是我的规则(已复制):

Options +FollowSymLinks -MultiViews# 打开 mod_rewrite重写引擎开启重写基础/测试## 隐藏 .php 扩展名# 外部重定向/dir/foo.php 到/dir/fooRewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]重写规则 ^ %1 [R,L,NC]## 内部转发/dir/foo 到/dir/foo.phpRewriteCond %{REQUEST_FILENAME}.php -f重写规则 ^ %{REQUEST_FILENAME}.php [L]

这条规则给出:

Forbidden 您无权访问.../Test/TestScript.php on this server."

所以最后一行改为:

RewriteRule ^.* http://localhost/Test/TestScript.php [L]

但现在得到这个错误:

页面没有正确重定向

希望这个地方的聪明人可以帮助我.谢谢.

解决方案

规则集以循环结束.让我们看看:

请求是http://localhost/Test/TestScript.php 被重定向到http://localhost/Test/TestScript/,让浏览器显示它,最后正在尝试映射回原始资源.

规则中的 [L] 标志不会像很多人认为的那样停止这个过程.重写引擎逐个规则地遍历完整的规则集,并且当特定规则匹配时,它遍历相应的条件(如果有).由于每个请求都会重复这个过程,并且规则会生成新的请求,因此很容易进入无限循环.

这就是消息页面没有正确重定向"在这种情况下的含义.

这里是这个过程的技术细节

一些解决方案:

I) 最好和更实用的方法是直接使用初始请求中的漂亮"URL,将其静默映射到资源.这是一个一步过程,其中漂亮"的 URL 始终显示在浏览器的地址栏中.此选项的优点之一是传入 URL 的 URI 路径中不必存在任何内容.

  1. 请求:http://localhost/Test/TestScript/
  2. 内部映射到资源:http://localhost/Test/TestScript.php

Options +FollowSymlinks -MultiViews重写引擎开启重写基数/# 防止循环RewriteCond %{REQUEST_URI} !\.php [NC]# 内部映射到资源,在地址栏中显示漂亮"的 URL重写规则 ^([^/]+)/([^/]+)/?/$1/$2.php [L,NC]

<小时>

II) 如果这是不可能的,因为已经有链接直接指向资源,一种显示漂亮" URL 但仍然从原始请求中获取数据的方法是首先可见且永久重定向,剥离扩展以显示漂亮"的 URL,然后在内部重写回原始资源.

  1. 请求:http://localhost/Test/TestScript.php,
  2. 永久且可见的重定向到:http://localhost/Test/TestScript/漂亮"的 URL,
  3. 内部和静默映射回:http://localhost/Test/TestScript.php,原始请求.

Options +FollowSymlinks -MultiViews重写引擎开启重写基数/# 直接从 THE_REQUEST 变量中获取 URI 路径RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/([^/]+)/([^.]+)\.php [NC]# 去除扩展名并永久重定向重写规则 .*/%2/%3/[R=301,L,NC]# 现在浏览器栏显示`http://localhost/Test/TestScript/`# 防止循环RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !\.php [NC]# 内部映射到原始资源重写规则 ^([^/]+)/([^/]+)/?/$1/$2.php [L,NC]

注意:

  1. 以上选项将放在根目录下的一个 .htaccess 文件中,确保启用 mod_rewrite.
  2. 字符串 TestTestScript 被假定为动态的,可以用任何名称替换.
  3. 主机名 localhost 是一个示例,也可以替换为任何其他名称.

I am not expert in htaccess files and I am trying something that seems very simple but I have not been able to do it.

I searched and finally find something here could work for my use but it didn't. The code is below.

Here is an example of what i need:

http://localhost/Test/TestScript.php to show this: http://localhost/Test/TestScript/ with script in original place.

These are my rules (Copied):

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /Test

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

This rule give:

Forbidden You don't have permission to access ".../Test/TestScript.php on this server."

So changed last line to:

RewriteRule ^.*  http://localhost/Test/TestScript.php  [L]

But now get this error:

The page isn't redirecting properly

Hope brilliant people in this place can help me. Thank you.

解决方案

The rule-set ends up in a loop. Let's see:

The request is http://localhost/Test/TestScript.php is redirected to http://localhost/Test/TestScript/, for the browser to show it, and finally is trying to be mapped back to the original resource.

The [L] flag in the rule doesn't stop the process, as many people think. The rewriting engine loops through the complete rule-set, rule by rule, and when a particular rule matches, it loops through the corresponding conditions, if any. As this process is repeated for each request and rules generate new requests, it is easy to enter an infinite loop.

That's what the message "The page isn't redirecting properly" means in this case.

Here are The Technical Details of this process

Some solutions:

I) The best and more practical one is to use directly the "pretty" URL in the initial request, mapping it silently to the resource. This is a one step process where the "pretty" URL is always displayed in the browser's address bar. One of the advantages of this option, is that nothing in the URI-path of the incoming URL has to exist.

  1. Request: http://localhost/Test/TestScript/
  2. Mapped internally to resource: http://localhost/Test/TestScript.php

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Prevent loops
RewriteCond %{REQUEST_URI} !\.php  [NC]
# Map internally to the resource, showing the "pretty" URL in the address bar
RewriteRule  ^([^/]+)/([^/]+)/?   /$1/$2.php  [L,NC]


II) If that's not possible because there are already links pointing directly to the resource, one way to show the "pretty" URL but still get the data from the original request, is to make a visible and permanent redirect first, stripping the extension to display the "pretty" URL, and then an internal rewrite back to the original resource.

  1. Request: http://localhost/Test/TestScript.php,
  2. Permanent and visible redirect to: http://localhost/Test/TestScript/ the "pretty" URL,
  3. Internal and silent mapping back to: http://localhost/Test/TestScript.php, the original request.

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/([^/]+)/([^.]+)\.php [NC]
# Strip the extension and redirect permanently
RewriteRule  .*   /%2/%3/   [R=301,L,NC]

# Now the browser bar shows `http://localhost/Test/TestScript/`

# Prevent loops
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php  [NC]
# Map internally to the original resource
RewriteRule  ^([^/]+)/([^/]+)/?   /$1/$2.php  [L,NC]

NOTES:

  1. The above options are to be placed in one .htaccess file at root directory, making sure mod_rewrite is enabled.
  2. Strings Test and TestScript are assumed to be dynamic and can be replaced with any name.
  3. Host name localhost is an example and can also be replaced with any other name.

这篇关于如何停止 .htaccess 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆