语言子文件夹上的htaccess 404错误(en /)

时间:2018-05-30 15:59:03

标签: php .htaccess https no-www

我有以下结构:

/  
/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]  

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你真的很接近你拥有的东西。现在我知道了这个问题,我会告诉你我使用的内容 - 你需要一个条件说不是目录 - 语法是!-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
相关问题