0

您好,我有以下适用于 IIS 6 的 isapi 重写规则。

它重写:

http://www.mysite.com/index.php?category=white-wine

到:

http://www.mysite.com/white-wine

.htaccess 内容:

RewriteEngine on
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+)/?$ /?category=$1 [NC,QSA,L]

我现在正在尝试执行几乎相同的规则,但在更深的文件夹中。 编辑 该规则在站点根目录中的同一个 .htaccess 文件中我只是想将上述规则应用于位于名为 products 的根目录中的文件夹中的 index.php 文件。

我想重写:

http://www.mysite.com/product/index.php?product=the-chosen-product 

到:

http://www.mysite.com/product/the-chosen-product

我试过这个:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+)/product/?$ /product/?product=$1 [NC,QSA,L]

尽管我的 php 页面上有很多错误,但这不起作用:

Warning: require_once() [function.require-once]: URL file-access is disabled in the server configuration

Warning: require_once(http://www.mysite.com/myfolder/framework/library.php) [function.require-once]: failed to open stream: no suitable wrapper could be found

我最初在第一条规则中遇到了这些错误,但是现在它正在起作用,我想一定有可能以某种方式执行第二条规则?

另外我将如何更改它而不是显示:

http://www.mysite.com/product/my-chosen-product 

它会显示:

http://www.mysite.com/product/my-chosen-product.htm

我对此真的很陌生。我浏览了许多其他帖子,我感到非常困惑,特别是我认为 isapi rewrite 与 mod_rewrite 的工作方式略有不同。

4

1 回答 1

1

RE: “警告:require_once() [function.require-once]:URL 文件访问被禁用...”。

不要使用require_once(http://www.mysite.com/myfolder/framework/library.php)——在你的服务器上是被禁止的(但即使它会被启用——也不建议这样做)。改为使用require_once($_SERVER['DOCUMENT_ROOT'] . '/myfolder/framework/library.php')


RE:重写规则(针对产品)。改用这个:

RewriteCond ${REQUEST_URI} !^/product/index\.php
RewriteRule ^product/([^/]+)/?$ /product/index.php?product=$1 [NC,QSA,L]

所以你的整个 .htaccess 应该是这样的:

RewriteEngine on
RewriteBase / 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?category=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^product/([^/]+)/?$ /product/index.php?product=$1 [NC,QSA,L]

我对您的规则做了一些小的更改:index.php立即指定,否则 IIS 稍后将不得不找出正确的脚本名称 .. 这需要一些很小但仍然需要的资源。


这些规则适用于无扩展名的 URL(例如http://www.mysite.com/product/the-chosen-product)。如果您想.htm在此类 URL 中添加扩展名,您必须执行以下操作:

a) 立即在您的应用程序中生成这些 URL

b) 稍微修改重写规则:将 RewriteRule 行替换为:

RewriteRule ^([^/]+)\.htm$ /index.php?category=$1 [NC,QSA,L]
RewriteRule ^product/([^/]+)\.htm$ /product/index.php?product=$1 [NC,QSA,L]

进行此类更改后,旧的无扩展名 URL(没有.htm)将不再起作用。

于 2011-07-08T16:22:07.337 回答