我正在一个懒散的网站上工作。
网站上填充了常规链接,这些链接会在.php
页面翻译成相应的.htaccess
页面。
就是这样:
RewriteEngine on
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
首先,我希望得到一些关于这个页面是否具有应有的一切的帮助。我以前从没搞过它。
其次和我的主要问题,例如,如果我写地址www.thewebsite.com/foobar.html
,它将被www.thewebsite.com/cat.php?cat=foobar
页面翻译成.htaccess
,它会给出一个数据库错误(并显示有关数据库的信息)。
我已检查cat.php
,检查该类别是否存在,但我无法将用户重定向到404错误页面。网站中有一个名为404.shtml
的网页,但是将用户重定向到该网页会导致.htaccess
再次将其更改为cat.php?cat=404
。
他们使用.htaccess
页面的方式是否正常?我应该改变这个系统吗?
用户如何发送到错误页面?从我的理解,服务器应该自己做吗?
我希望得到一些澄清......关于这个我不太了解的主题有很多。
更新
这是我的新.htaccess页面
RewriteEngine on
RewriteRule ^error.php?err=(.*)$ Error$1.html
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
因为重定向在代码中并且用户无法看到它,所以我允许自己以非干净的方式编写链接。我尝试将其转换为干净的URL,但以下内容没有做任何事情:
RewriteRule ^error.php?err=(.*)$ Error$1.html
有人可以帮我理解为什么吗?我认为既然error.php是一个真实的页面,我应该把它放在条件之前,但它没有用。顺便说一句,我在一篇关于.htaccess的文章中看到该页面应该以{{1}}开头。在我看来,每个人都有自己的写作方式。是否有指南或类似的东西,我可以肯定它是真实的,涵盖了所有关于.htaccess的基础?
非常感谢!!
答案 0 :(得分:1)
你的正则表达式在任何地方都是错误的。需要转义文字点,否则它将匹配任何字符。此外,最好使用L
和QSA
标记来正确结束每个规则。
RewriteEngine on
RewriteBase /
RewriteRule ^koral/([^/]+)/?$ page.php?name=$1 [L,QSA]
RewriteRule ^([^.]+)\.html/([^/]+)/([^/]+)/([^/]*)/?$ cat.php?cat=$1&page=$2&order=$3&dir=$4 [L,QSA]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^.]+)\.html$ product.php?cat=$1&product=$2 [L,QSA]
答案 1 :(得分:1)
根据我的经验,使用重写规则来处理不存在的.html
页面的链接是不寻常的,但它实际上只是对“漂亮”网址的不同看法,例如: www.thewebsite.com/foobar/
在后端被路由到cat.php?cat=foobar
。
您的404问题有所不同。您需要能够显示错误页面。
此处的一个选项是重写请求,只要它们不请求现有文件即可。这对于提供诸如图像,CSS文件等的静态内容是非常常见的。为此,您可以使用-d
and -f
options to RewriteCond
,它分别在请求目录和文件时适用:
RewriteEngine On
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
现在,对404.shtml
的请求应该通过,因为您正在请求文件系统上的现有文件。
请注意,RewriteCond
仅适用于紧随其后的单个RewriteRule
。对于其他RewriteRule
,还包括其他RewriteCond
s。