我们有一个具有以下文件结构的网站:
/www
/nl
index.php
contact.php
...
/fr
index.php
contact.php
...
/de
index.php
contact.php
...
index.php
.htaccess
index.php
文件在 www
文件夹是一个语言选择页面,链接到 index.php
nl
中的文件, fr
和 de
文件夹。后面的 index.php 页面构建网页并从 <page>.php
加载页面内容.
过去,页面是这样加载的:
http://www.ourdomain.com/nl/index.php?page=contact
这将加载 /nl/contact.php
的内容进入网站结构。
为了使网站对 SEO 更加友好,我们将其更改为:
http://www.ourdomain.com/nl/contact
这可以达到 3 层深 ( /nl/nav-level1/nav-level2/nav-level3
)
nl
里面的index.php文件, fr
和 de
文件夹,有一个算法,对于从旧 url 访问该站点的人,301 将旧页面 url 重定向到新页面 url。
header( 'HTTP/1.1 301 Moved Permanently' );
header('location:' . $newurl);
exit;
根 www 目录中的 .htaccess 文件如下所示:
RewriteEngine On
# RewriteBase wijzigen in / (online)
RewriteBase /
# PDF: redirect pdf file in wrong directory to root language directory (nl/fr/en)
RewriteRule ^(nl|fr|de)(/[a-zA-Z0-9\-/]+)+/([a-zA-Z0-9\-]+.pdf)/?$ $1/$3 [NC,L,R=301]
# pages: redirect index.php in wrong directory to the correct language directory (nl/fr/en)
RewriteRule ^(nl|fr|de)(/[a-zA-Z0-9\-/]+)+/(index.php)/?$ $1/$3 [NC,L]
# pages: convert SEO friendly url to index.php?page=<page>
RewriteRule ^(nl|fr|de)(/[a-zA-Z0-9\-/]+)*/([a-zA-Z0-9\-]+)/?$ $1/index.php?page=$3 [NC,L]
在我将此网站移至我们新托管服务提供商的服务器之前,一切正常。
现在,当我转到 http://www.ourdomain.com/nl/contact
, 而不是加载 http://www.ourdomain.com/nl/index.php?page=contact
, http://www.ourdomain.com/nl/contact.php
是直接加载的。这意味着我可以看到页面的内容,但看不到我们网站的其他结构和布局。
我检查了是否 .htaccess
文件作品,作者:
- 将我的 .htaccess 内容放入 http://htaccess.mwl.be/ 的 .htaccess 测试器中并测试我网站的网址:这返回了预期的网址
- 在 .htaccess 文件中放入无效内容:如预期的那样返回服务器错误
- 将所有内容重定向到测试页面:这会将所有 url 重定向到测试文件。
然后我发现我可以通过在语言文件夹(nl
、fr
、de
)中创建与 php 文件同名的文件夹来解决这个问题。这使得 seo 友好的 url 按预期加载页面,但不知何故,get 变量仍然添加到 seo 友好的 url。
/www
/nl
/contact
/...
index.php
contact.php
...
/fr
/contact
/...
index.php
contact.php
...
/de
/contact
/...
index.php
contact.php
...
index.php
.htaccess
而不是 http://www.ourdomain.com/nl/contact
我总是被重定向到 http://www.ourdomain.com/nl/contact/?page=contact
如果我去 http://www.ourdomain.com/nl/contact/
get 变量没有添加到 url。
我已经在这个问题上工作和搜索了几个小时,但我没有弄清楚它。任何有值(value)的输入将不胜感激。
最佳答案
我刚刚找到了问题的答案。
显然,MultiViews
是在新服务器上设置的。因此,不带扩展名的文件名与带扩展名的文件匹配,导致 http://example.com/nl/contact
加载 http://example.com/nl/contact.php
.
关于php - .htaccess RewriteRule 不工作,加载 <name>.php 而不是 index.php?page=<name>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980909/