php - 语言子文件夹上的 htaccess 404 错误(en/)
问题描述
我有以下结构:
/
/about.php
/contact.php
/en/
/en/about.php
/en/contact.php
我想从网址中删除 .php 扩展名和 www 前缀并强制使用 https。
现在我有以下htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
提前感谢您的帮助!
解决方案
就你所拥有的而言,你真的很接近。既然我知道了问题,我会给你我使用的东西——你需要一个条件,说不是目录——语法是!-d
这就是我的htaccess 的外观(因为我使用它来删除html
以及php
:
RewriteEngine on
# Redirect www and http to https - non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# Start php extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# End php extension remove
# Start html extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# End html extension remove
如您所见,第一个条件检查它是否不是目录。以下条件检查它的文件扩展名是否为.php
. 如果两个条件都为真——规则是删除扩展。
作为一个注释——我会让你的语法更简洁一些,并将你正在尝试做的事情的“部分”分开。
根据评论更新
要删除https
强制 - 只需注释掉第一个条件并在规则中更改https
为:http
RewriteEngine on
# Redirect www and http to https - non-www
#RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Start php extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# End php extension remove
# Start html extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# End html extension remove
推荐阅读
- validation - .Net Core 2.2 中的 RazorViewTest;@Html.ValidationMessage("key") 不显示任何内容
- sql-server - 在没有链接服务器的单独服务器上运行临时 SQL 代码
- java - 我想不出移动球的方法
- mysql - 一些不知道怎么写的选集
- javascript - 需要让程序知道选择了哪些值并根据选择的值吐出一个特定的对象
- swift - 有什么方法可以解释 GPS ping 延迟并预测用户的位置,无论用户速度有多快?
- react-native - 找不到变量:AppRegistry - 如果它被导入
- plsql - 在一个循环中连接两个字符串的值
- python - numpy.linalg.lstsq 在不同处理器上的不同结果
- multithreading - 以下并发Scala程序的输出有什么区别