PHP-extensie verwijderen van website-URL's
Handleiding voor het configureren van uw webserver (Apache/Nginx) om de .php-extensie van URL's te verbergen voor schonere links.
Introductie
In plaats van 'https://example.com/about.php', wil je 'https://example.com/about'. Dit ziet er professioneel uit en is beter voor SEO.
Apache (.htaccess)
Voeg het volgende toe aan uw .htaccess-bestand in de hoofdmap:
RewriteEngine On
# 1. Externally redirect /file.php to /file (SEO friendly)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# 2. Internally rewrite /file to /file.php (Server handling)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Nginx (nginx.conf)
Voeg dit toe in uw server-blok of locatie-blok:
location / {
try_files $uri $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# Process PHP files
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
Belangrijke overwegingen
- Links: werk uw HTML-links handmatig bij. Wijzig
<a href="contact.php">in<a href="contact">. - Conflicten: Zorg ervoor dat u geen map heeft met de naam
contacten een bestand met de naamcontact.php, aangezien dit verwarring veroorzaakt.