Reputation: 406
I have an apache2 PHP server with the following .htaccess file in the public html root folder :
<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>
It contains a RewriteRule in order to redirect URLs like this :
However we noticed that when a client requests URLs without using the www subdomain, the server redirects to the https/www URL but adds .php extension at the end of the URLs like this :
This does not happen when requesting the url using the www subdomain :
So I figured the web server was adding the file extension to the URLs because of that RewriteRule in the .htaccess.
Can someone help me edit my .htaccess file so the redirection stops adding .php file extension at the end of URLs ? By the way, server is behind Cloudflare proxy. Thanks !
Here is the .conf file :
<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>
Upvotes: 0
Views: 93
Reputation: 320
It might be worth splitting up your rewrite rules in order to troubleshoot things a little easier (not tested).
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
Upvotes: 0