.htaccess文件问题

时间:2019-01-01 22:37:55

标签: php html apache .htaccess

我有2个要实现的目标。第一个是确保用户始终使用https,第二个是完全摆脱.php扩展名,因为我的网页中的每个人都使用此扩展名。

我的网站.htaccess文件是错误的,我不确定如何修复它。 它从以下网址重定向: www.mysite.com/login.php到www.mysite.com/mysite.com/login

第二个损坏的URL设法消除了所有PHP,但在其内部连接了我的整个网站...

我尝试了在各种站点上找到的其他htaccess“ php”文件扩展名删除器,但是它们不起作用或仅部分起作用(仅我的某些网页将.php删除了)。我想我必须在我的.htaccess文件中包含一个不需要的额外内容,但是我对配置此文件非常不熟悉,因此每次我直接将其更改为500错误页面时...

Header add Strict-Transport-Security "max-age=31415926;includeSubDomains;"

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

# remove the .php extension 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]

# redirect from .php to less php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]

有人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

要从网址中删除.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

答案 1 :(得分:0)

类似这样的东西:

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


# add the .php extension (so the server can process it)
RewriteCond %{REQUEST_FILENAME} !-d  #is not a directory
RewriteCond %{REQUEST_FILENAME} !-f  #is not a file
#We don't want to add PHP to things like .js or .png or .php files.
#So if it's a real file we can stop here.
#If it's not just add .php
RewriteRule (.+) $1.php [L]

# remove the .php extension and redirect to clean URL
RewriteCond %{REQUEST_URI} \.php$  #ends in .php
RewriteRule (.+)\.php $1 [R=301,L]

Online Tester-不幸的是,我没有保存测试的方法。这是一个非常不错的测试器,但是它不能处理!-f条件,因此请记住这一点。显然,在测试人员域中实际上不存在某些组合的URL。

无论如何,当您输入如下网址时:

www.example.com/somepage

服务器无法运行该服务器,HTACESS所做的只是在幕后,它添加了.php扩展名并停止了L。因此,如果该URL不以真实文件或目录的形式存在(在执行重写规则之前),则服务器看到的URL是www.example.com/somepage.php

但是,当URL像这样www.example.com/somepage.php时,HTACCESS会删除.php并执行301 R=301并停止L。重定向后,第一个规则将.php重新添加到URL中,但在服务器上“仅”添加,因此在浏览器中,URL将不包含PHP扩展名。

希望如此。