Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite Adds .php Extension

My .htaccess file is as follows:

Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

The issue that I am having is that when I try to access my site without the www. prefix, the .php extension is added to the address, which can often cause a problem. For example, if I try to access my homepage with the address example.com, that address is transformed into www.example.com/.php. I want the www. to be added, but the .php extension added at the end just causes an error. How do I fix this?

like image 601
user532493 Avatar asked Nov 14 '22 03:11

user532493


1 Answers

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Remove the L in the [R=301,L], which means "Last", which means "stop processing RewriteRules after this one."

The 301 also means "redirect permanently." In this case, your browser will remember the permanent redirect. Is it possible that you had one version of your rewrite rules, went to your URL, and then changed it? Clear your cache or try another browser.

like image 132
Dondi Michael Stroma Avatar answered Dec 25 '22 07:12

Dondi Michael Stroma