重定向到https但没有.php

时间:2017-04-14 17:05:35

标签: regex https

现在我有一个https。我需要在.htaccess中重定向。我能找到这个:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但我发现如果用户写道:

http://myDomain/someFile

重定向到:

https://myDomain/someFile.php

我认为正确的应该没有.php 怎么做?

这些都是规则:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

5 个答案:

答案 0 :(得分:1)

这是您的问题的工作代码。我在我的服务器上检查了这个,它运行正常

RewriteEngine on
RewriteBase /

#for https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

以下是解释上述代码的链接 - Explain htaccess rules?

答案 1 :(得分:0)

关闭MultiViews以获取.htaccess。选项Apache's content negotiation module(请参阅http://httpd.apache.org/docs/2.4/content-negotiation.html)由mod_rewrite使用之前 /file,并使Apache服务器匹配文件扩展名。因此,如果/file.php是网址,那么Apache将提供www

您还可以将http301规则合并为一条规则,以避免多次Options -MultiViews RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L] 重定向。

from random import random
random.sample(list_variable,number)

确保在测试此更改之前清除浏览器缓存。

答案 2 :(得分:0)

如果您使用php,那么您可能拥有Apache。你应该找到你的.conf文件。我有

/etc/apache2/sites-available/000-default.conf (Kubuntu 16.04.)

并更改虚拟主机设置:

<VirtualHost *:80>
    . . .

    Redirect "/" "https://your_domain_or_IP/"

    . . .
</VirtualHost>

答案 3 :(得分:0)

在您的 .htaccess (在您的 public_html 的根目录下)中点击此内容 p>

#FORCE HTTPS CONNECTION
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourDomain.com/$1 [R=301,L]
#FORCE HTTPS CONNECTION

让我知道它是否成功;)

答案 4 :(得分:0)

如果要将http重定向到https,请使用以下重定向代码:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourDomain.com/$1 [R=301,L]