【发布时间】:2013-01-13 14:00:48
【问题描述】:
我在本地主机上使用重写引擎时遇到了一些问题。
我正在我的www/project1/ 文件夹中的一个站点上工作,我正在尝试使用重写引擎。但是我遇到了一些问题:
首先这是我的 .htaccess 文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile$ ./index.php?p=userprofile
RewriteRule ^profile/(.*)$ ./index.php?p=userprofile&id=$1 [L]
RewriteRule $(.*)$ ./index.php&c=$1
基本上我想重写
所有/profile 链接到index.php?p=userprofile
将所有/profile/5链接重写为index.php?p=userprofile&id=5
和所有其他人(例如/somepage)到index.php?c=somepage
当使用上面的 .htaccess 时,我首先得到一个 500 内部服务器错误。但是,当删除最后一个重写时,此错误就会消失。
删除最后一条规则后,又出现了另一个问题
当转到localhost/project1/profile 时,一切正常,但是当添加另一个参数时:localhost/project1/profile/5 我所有的 css 都消失了。
我的 CSS 包含如下:href="style/main.css"
因此文件位于localhost/project1/style/main.css
现在我尝试通过添加/ 使路径相对
但是 CSS 根本没有应用,index.php 和 project1/profile 上都没有
有什么解决方案可以让我的 3 条规则发挥作用吗?
【问题讨论】:
-
首先,为什么在所有重写路径的开头都有一个点?那些应该以斜杠
/index.php...开头,而不是./index.php...并且/index.php&c=$1应该是/index.php?c=$1。这些只是这里的错字还是在您的实际配置中?关于 RewriteCond,它仅适用于紧随其后的 RewriteRule,不适用于后续规则,因此如果要对其他两个规则进行有效文件检查,则需要在每个规则前面再次添加 RewriteCond它们(可能是您的 CSS 文件和其他 500 错误的问题)。
标签: apache .htaccess mod-rewrite