首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >htaccess显示404错误,而不是查找index.php

htaccess显示404错误,而不是查找index.php
EN

Stack Overflow用户
提问于 2021-11-08 21:03:42
回答 1查看 36关注 0票数 0

我有一些关于可疑URL重写的问题

我想用我的.htaccesshttp://localhost:81/es/index.php变成http://localhost:81/index.php?lengua=es,以帮助页面搜索引擎优化

这是我当前的.htaccess

代码语言:javascript
复制
<FilesMatch ".*\.(log|ini|htaccess)$">
    deny from all
</FilesMatch>

Options -Indexes
RewriteEngine On
RewriteBase /
FallbackResource "index.php"

RewriteRule ^(en|es|pt)?/?(.*)?$ $2?idioma=$1 [QSA,L]

我已经检查了它们是否可以与htaccess tester一起工作,并且工作正常,但是当我浏览页面时,它显示“找不到文件”。错误(我有index.php,我没有es/index.php)

因为我的输出URL是http://localhost:81/index.php?lengua=es,所以我不明白为什么它不工作

EN

回答 1

Stack Overflow用户

发布于 2021-11-11 15:10:46

我建议将其分为四个重写规则:

代码语言:javascript
复制
RewriteEngine on

# Redirect to add the trailing slash to language directory
# http://example.com/es  > http://example.com/es/
RewriteRule ^/?(en|es|pt)$ /$1/ [R=301,L]

# Redirect to remove `index.php`
# http://example.com/es/index.php  > http://example.com/es/
RewriteRule ^/?(en|es|pt)/index\.php$ /$1/ [R=301,L]

# Handle requests for the base language directory
# http://example.com/es/  > http://example.com/index.php?idioma=es
RewriteRule ^/?(en|es|pt)/$ /index.php?idioma=$1 [QSA,L]

# Handle requests for php files within the language directory
# http://example.com/es/foo.php  > http://example.com/foo.php?idioma=es
RewriteRule ^/?(en|es|pt)/(.+\.php)$ /$2?idioma=$1 [QSA,L]

我会删除RewriteBase /,因为我相信这是根.htaccess文件中的默认设置。

我会删除FallbackResource "index.php",因为根据您提供的示例,您不应该需要它。如果保留它,文档中的示例将显示它以斜杠:FallbackResource /index.php开头。您也应该在没有它的情况下进行测试,因为它有可能与重写规则冲突。

我总是喜欢在重写规则时使用一个可选的斜杠^/? (而不仅仅是^),这样无需修改就可以在.htaccesshttpd.conf中使用它们。

问题中的规则使所有内容都是可选的,包括语言代码。我的规则使用(en|es|pt)而不是(en|es|pt)?,这样如果语言代码不在URL中,它们就不会匹配。

我的规则不是将语言目录后面的斜杠设置为可选的,而是在它存在和不存在时做不同的事情。

在您的规则中,(.*)?完全等同于更简单的(.*)。我将其更改为(.*\.php),以便它只匹配PHP文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69889926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档