您好我在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文件中,我只想将上述规则应用于index.php文件,该文件位于根目录中名为products的文件夹中。
我想改写:
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重写,我认为,与mod_rewrite的工作方式略有不同。
答案 0 :(得分: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将不得不在以后找出正确的脚本名称..这需要一些微小但仍有资源来执行此操作。
这些规则适用于无扩展名的网址(例如http://www.mysite.com/product/the-chosen-product
)。如果您希望在此类网址中使用.htm
个扩展程序,则必须执行以下操作:
a)立即在您的应用程序中生成这些URL
b)稍微修改重写规则:用以下代码替换RewriteRule行:
RewriteRule ^([^/]+)\.htm$ /index.php?category=$1 [NC,QSA,L]
RewriteRule ^product/([^/]+)\.htm$ /product/index.php?product=$1 [NC,QSA,L]
通过此类更改,旧的无扩展名网址(不包含.htm
)将不再有效。