我有一些关于可疑URL重写的问题
我想用我的.htaccess把http://localhost:81/es/index.php变成http://localhost:81/index.php?lengua=es,以帮助页面搜索引擎优化
这是我当前的.htaccess
<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,所以我不明白为什么它不工作
发布于 2021-11-11 15:10:46
我建议将其分为四个重写规则:
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开头。您也应该在没有它的情况下进行测试,因为它有可能与重写规则冲突。
我总是喜欢在重写规则时使用一个可选的斜杠^/? (而不仅仅是^),这样无需修改就可以在.htaccess和httpd.conf中使用它们。
问题中的规则使所有内容都是可选的,包括语言代码。我的规则使用(en|es|pt)而不是(en|es|pt)?,这样如果语言代码不在URL中,它们就不会匹配。
我的规则不是将语言目录后面的斜杠设置为可选的,而是在它存在和不存在时做不同的事情。
在您的规则中,(.*)?完全等同于更简单的(.*)。我将其更改为(.*\.php),以便它只匹配PHP文件。
https://stackoverflow.com/questions/69889926
复制相似问题