.htaccess重写规则将.php扩展名添加到URL

xnifntxz  于 2023-08-06  发布在  PHP
关注(0)|答案(1)|浏览(127)

我有一个apache 2 PHP服务器,在公共html根文件夹中包含以下.htaccess文件:

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
    RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>

字符串
它包含一个RewriteRule,以便像这样重定向URL:

然而,我们注意到,当客户端在不使用www子域的情况下请求URL时,服务器会重定向到https/www URL,但会在URL的末尾添加.php扩展名,如下所示:

使用www子域请求url时不会发生这种情况:

因此,我认为Web服务器正在将文件扩展名添加到URL中,因为. htaccess中的RewriteRule。
有人可以帮助我编辑我的.htaccess文件,以便重定向停止在URL末尾添加.php文件扩展名吗?顺便说一下,服务器位于Cloudflare代理之后。谢谢你,谢谢
下面是.conf文件:

<IfModule mod_ssl.c>
<VirtualHost *:443>
     ServerName {domain.com}
     ServerAlias www.{domain.com}
     ServerAdmin webmaster@{domain.com}
     DocumentRoot /var/www/{domain.com}/public_html

     <Directory /var/www/{domain.com}/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
     </Directory> 
     #ci-dessous remoteip de cloudflare
     RemoteIPHeader CF-Connecting-IP
     ErrorLog ${APACHE_LOG_DIR}/{domain.com}-error.log
     CustomLog ${APACHE_LOG_DIR}/{domain.com}-access.log combined
     RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.

#      RewriteCond %{SERVER_NAME} ={domain.com} [OR]
#      RewriteCond %{SERVER_NAME} =www.{domain.com}
#      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/{domain.com}/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/{domain.com}/privkey.pem
    AllowEncodedSlashes On
    SSLProxyEngine on
    <Location "/blog">
        # Replace with your subdomain, https matters here https://ghost.org/help/apache-subdirectory-setup/
        ProxyPass https://{blog-domain}/blog
    </Location>
</VirtualHost>
</IfModule>

zc0qhyus

zc0qhyus1#

为了更容易地排除故障(未测试),可能值得将重写规则分开。

RewriteEngine On

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

# Always use www
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC] 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]

# Remove the need for .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

字符串

相关问题