我刚刚开始使用 mod_rewrite,这是我用于具有多种语言支持的非常基本的结构化网站:
RewriteEngine on
ErrorDocument 404 /error404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/([^/]+)$ $2.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [L]
我的想法是语言在 URl 的开头指示并且需要是两个字符(az 或 AZ)。之后会有一个字符串引用一个 php 文件,该文件与该字符串具有相同的文件名,只是附加了 .php。语言将作为 GET-Variable ( ?lang=en
) 添加。
所以,一个 URl 可能看起来像这样:/en/index
然后应该被重定向到index.php?lang=en
.
如果 URl 不是以语言开头,而是直接以字符串开头,则不会将 GET 变量附加到字符串。例如/index
应该参考index.php
。如果没有字符串,则应使用索引作为默认值。
到现在为止还挺好。工作正常。只要我输入一个字符串(无论我是否使用 2 个字符作为语言),该站点总是显示 500 Internal Server Error,而不是转到error404.php
. 另外,如果我在.htaccess 中删除这一行(404),它仍然是同样的错误。
所以,我假设 .htaccess 中的其他行有问题导致此错误,有人知道这可能是什么吗?
非常感谢任何帮助!