首页 > 解决方案 > 语言子文件夹上的 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]  

提前感谢您的帮助!

标签: php.htaccesshttpsno-www

解决方案


就你所拥有的而言,你真的很接近。既然我知道了问题,我会给你我使用的东西——你需要一个条件,说不是目录——语法是!-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

推荐阅读