user532493
user532493

Reputation: 345

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?

Upvotes: 5

Views: 165

Answers (2)

Dondi Michael Stroma
Dondi Michael Stroma

Reputation: 4800

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.

Upvotes: 1

michaelhagedon
michaelhagedon

Reputation: 40

The "L" flag on line 4 of your file means "last", i.e., it won't process any more rules. Try removing the L.

Upvotes: 0

Related Questions