需要帮助在.htaccess中使用mod_rewrite合并两个重写规则

时间:2012-02-15 18:59:35

标签: .htaccess mod-rewrite

我需要将两个重写条件组合成一个我在网上搜索的条件。问题是,如果一个人工作,另一个人不会:

### [RULE1: remove the .php at end of file / url] 
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

### [RULE2: http to https redirection]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST}
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

显然,无论哪种规则都放在顶级作品中。

所以我需要一种方法来删除url末尾的php扩展,但是如果url包含http而不是https,它还应该自动重定向到https版本的脚本。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

好像您正在设置要通过https访问的每个页面。我在下面纠正了一些错误。这样做:

#rule to make both WORK
 RewriteCond %{HTTPS} on
### [RULE1: remove the .php at end of file / url] 
# If requested URL-path plus ".php" exists as a file
 RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
 RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

### [RULE2: http to https redirection]
 RewriteCond %{HTTPS} off
#The next line is not required.
#RewriteCond %{HTTP_HOST}
 RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]