首页 > 解决方案 > .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]

有人有想法么?

标签: phphtmlapache.htaccess

解决方案


像这样的东西:

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。

无论如何,当您输入这样的 URL 时:

www.example.com/somepage

服务器无法运行它,HTACESS 所做的是在幕后添加.php扩展并停止L。所以服务器看到的 URL 是这个www.example.com/somepage.php URL 不作为真实的文件或目录存在(在执行重写规则之前)。

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

希望这是有道理的。


推荐阅读