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

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

问题描述

我不是专家htaccess的文件,我想的东西,看起来很简单,但我一直没能做到这一点。

我搜查,终于找到的东西在这里可以为我的使用,但事实并非如此。在code是如下。

下面是什么,我需要一个例子:

的http://localhost/Test/TestScript.php 来显示这一点:的http://本地主机/测试/ TestScript / 在原来的地方脚本。

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

 选项+了FollowSymLinks -MultiViews
#开启mod_rewrite的上
RewriteEngine叙述上
的RewriteBase /测试

##隐藏PHP扩展
#要外部重定向/dir/foo.php到/ DIR /富
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S([^。] +)\。PHP [NC]
重写规则^%1 [R,L,NC]

##在内部前进/ DIR /富到/dir/foo.php
的RewriteCond%{} REQUEST_FILENAME -f .PHP
重写规则^%{} REQUEST_FILENAME的.php [L]
 

本规则给予:

紫禁城您没有权限访问... /测试/ TestScript.php此服务器上。

因此​​改变最后一行:

 重写规则^ * HTTP://localhost/Test/TestScript.php [L]
 

但是,现在收到此错误:

的网页没有正确重定向

希望灿烂的人在这个地方可以帮助我。谢谢你。

解决方案

规则集结束了在一个循环。让我们来看看:

请求是的http://localhost/Test/TestScript.php 将被重定向到的http://本地主机/测试/ TestScript / ,系统的浏览器,以显示它,最后试图映射回原始资源

在[L]标志的规则不会停止的过程中,许多人认为。重写引擎循环通过规则完整的规则集,规则,当一个特定的规则匹配,它循环通过相应的条件,如果有的话。作为这一过程被重复为每个请求和规则生成新的请求,就容易进入无限循环。

这是消息的的网页没有正确重定向的拿什么在这种情况下。

下面是这个过程中的技术细节

一些解决方案:

我)最好的和更实际的是可以直接用pretty的URL在初始请求,默默地其映射到资源。这是这里的pretty的URL始终显示在浏览器的地址栏一步的过程。其中一个这个选项的好处是,没有在输入网址的URI路径必须存在

  1. 请求:的http://本地主机/测试/ TestScript /
  2. 在内部映射到资源:的http://localhost/Test/TestScript.php

选项+了FollowSymLinks -MultiViews RewriteEngine叙述上 的RewriteBase / #prevent循环 的RewriteCond%{REQUEST_URI}!\。PHP [NC] #内部地图资源,显示在地址栏中的pretty的网址 重写规则^([^ /] +)/([^ /] +)/? /$1/$2.php [L,NC]


二)如果因为目前已经有直接链接指向的资源是不可能的,一种方式展现了pretty的网址,但仍从原来的请求获取数据,是使一个可见和永久重定向第一,剥离扩展,以显示pretty的URL,然后内部重写回原始资源

  1. 请求:的http://localhost/Test/TestScript.php
  2. 永久可见重定向到:的http://本地主机/测试/ TestScript / 的pretty的URL,
  3. 内部和沉默映射回:的http://localhost/Test/TestScript.php ,原来的要求

选项+了FollowSymLinks -MultiViews RewriteEngine叙述上 的RewriteBase / #直接从THE_REQUEST变量获取的URI路径 的RewriteCond%{THE_REQUEST} ^(GET | HEAD)。\ S /([^ /] +)/([^] +)\ PHP [NC] #剥开扩展和重定向永久 重写规则。* / 2%/ 3%/ [R = 301,L,NC] #现在浏览器栏显示'的http://本地主机/测试/ TestScript /` #prevent循环 的RewriteCond%{} REQUEST_FILENAME!-d 的RewriteCond%{REQUEST_URI}!\。PHP [NC] #内部映射到原始资源 重写规则^([^ /] +)/([^ /] +)/? /$1/$2.php [L,NC]

注:

  1. 在上面的选项将被放置在一个.htaccess文件的根目录下,确保的mod_rewrite 已启用。
  2. 字符串的测试的和的 TestScript 的假设是动态的,可以替换为任何名称。
  3. 主机名的本地主机的是一个例子,也可以换成任何其他名称。

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天全站免登陆