Forum Moderators: phranque
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L]
DirectoryIndex index.php
Options +FollowSymLinks
Options -Indexes
Options -MultiViews
AddHandler application/x-httpd-php5 .html .htm
AddHandler server-parsed .htm
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/JWJforum/$
RewriteRule ^([A-Za-z0-9-]+)\/?$ $1.php [NC] [edited by: phranque at 10:45 am (utc) on Apr 11, 2021]
[edit reason] please use example.net [/edit]
RewriteCond %{REQUEST_URI} !^/JWJforum/$It’s been a while since we indulged in a really good chorus of
RewriteRule ^([A-Za-z0-9-]+)\/?$ $1.php [NC]
The https/canonical rewrite rule should be the last rule in your file. Rules that deal with a single URL at a time (such as the clean URL rule) should be before that rule. The error document and Options should not be in the middle of the rewrite rules.
Obviously you are not rewriting to a physical file, because there would have to be an infinite number of them on the server.
In other words /paXge or /paXge.php display "file not found" but not the custom 404 page, whereas /page.phXp displays the 404 page correctly.
ErrorDocument 404 default
ErrorDocument 404 /404.php
If the ".php" file doesn't exist then you just get a 404.Oddly enough, in my previous post I wasn't thinking about extensionless URLs, but of an all-purpose rewrite like a CMS would do. Oops. Yeah, if you have a finite number of pages, and those are the only ones you get requests for, then you can certainly rewrite to add/remove an extension.
if a misspelling is with the suffic of the url, the custom 404 works fine, but if it's the page name that's misspelt then it doesn't. In other words /paXge or /paXge.php display "file not found" but not the custom 404 page, whereas /page.phXp displays the 404 page correctly.This is puzzling, because that final rule--the one that really shouldn't be the final rule, but that's a different issue--is written to only act on extensionless URLs. Is there another rule somewhere else that deals with php requests? Is there a point where requests get routed to a different directory, with a different htaccess?
# added 21/3/17 to redirect all http calls to https
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L]
# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|JPG|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
RewriteEngine on
# Inserted 22/11/07
#This section rewrites calls to example.net/index.htm as example.net/
#having first set the default homepage as 'index.php'.
#It doesn't screw up the subdomains as 301 redirects do.
#This is a link to the original post in WebmasterWorld
# https://www.webmasterworld.com/forum92/6012.htm
#
DirectoryIndex index.php
Options +FollowSymLinks
#next line prevents directory listings of all folders
Options -Indexes
Options -MultiViews
AddHandler application/x-httpd-php5 .html .htm
AddHandler server-parsed .htm
# to set the 404 page to a php version rather than the shtml default
ErrorDocument 404 /404.php
#####################################################
# Make clean url's
#####################################################
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/JWJforum/$
RewriteRule ^([A-Za-z0-9-]+)\/?$ $1.php [NC]
######################################################
#----all in this section due to 404's ---------#
######################################################
Redirect permanent /fuchsia_cuttings.php https://www.example.net/fuchsia-cuttings
Redirect permanent /fuchsia_care.php https://www.example.net/fuchsia-care
Redirect permanent /fuchsia_overwinter.php https://www.example.net/fuchsia-overwinter
Redirect permanent /pencil.php https://www.example.net/pencil3
Redirect permanent /Art-Forum.php https://www.example.net/ArtForum
Redirect permanent /art.php https://www.example.net/galleries
Redirect permanent /wip-bridlifeboat.php https://www.example.net/WIP-BridLifeboat.php
Redirect permanent /penandwash.php https://www.example.net/penandwash2.php
# make SE friendly urls for php pages using variables
RewriteRule poems/(.*)/(.*)/$ /poems.php?$1=$2 [R=301,L]
RewriteRule poems/(.*)/(.*)$ /poems.php?$1=$2 [R=301,L]
# old style art pages with trailing slash
ReWriteRule watercolours1/painting/(.*)/$ https://www.example.net/watercolour1.php?autoload=$1 [R=301,L]
ReWriteRule watercolours2/painting/(.*)/$ https://www.example.net/watercolour1.php?autoload=$1 [R=301,L]
ReWriteRule watercolours3/painting/(.*)/$ https://www.example.net/watercolour2.php?autoload=$1 [R=301,L]
ReWriteRule watercolours4/painting/(.*)/$ https://www.example.net/watercolour2.php?autoload=$1 [R=301,L]
ReWriteRule watercolours5/painting/(.*)/$ https://www.example.net/watercolour3.php?autoload=$1 [R=301,L]
ReWriteRule watercolours6/painting/(.*)/$ https://www.example.net/watercolour3.php?autoload=$1 [R=301,L]
ReWriteRule watercolours7/painting/(.*)/$ https://www.example.net/watercolour4.php?autoload=$1 [R=301,L]
RewriteRule pencil1/painting/(.*)/$ https://www.example.net/pencil1.php?autoload=$1 [R=301,L]
RewriteRule pencil2/painting/(.*)/$ https://www.example.net/pencil1.php?autoload=$1 [R=301,L]
RewriteRule pencil3/painting/(.*)/$ https://www.example.net/pencil2.php?autoload=$1 [R=301,L]
RewriteRule pencil4/painting/(.*)/$ https://www.example.net/pencil2.php?autoload=$1 [R=301,L]
RewriteRule pen1/painting/(.*)/$ https://www.example.net/pen1.php?autoload=$1 [R=301,L]
# old style art pages WITHOUT trailing slash
ReWriteRule watercolours1/painting/(.*)$ https://www.example.net/watercolour1.php?autoload=$1 [R=301,L]
ReWriteRule watercolours2/painting/(.*)$ https://www.example.net/watercolour1.php?autoload=$1 [R=301,L]
ReWriteRule watercolours3/painting/(.*)$ https://www.example.net/watercolour2.php?autoload=$1 [R=301,L]
ReWriteRule watercolours4/painting/(.*)$ https://www.example.net/watercolour2.php?autoload=$1 [R=301,L]
ReWriteRule watercolours5/painting/(.*)$ https://www.example.net/watercolour3.php?autoload=$1 [R=301,L]
ReWriteRule watercolours6/painting/(.*)$ https://www.example.net/watercolour3.php?autoload=$1 [R=301,L]
ReWriteRule watercolours7/painting/(.*)$ https://www.example.net/watercolour4.php?autoload=$1 [R=301,L]
RewriteRule pencil1/painting/(.*)$ https://www.example.net/pencil1.php?autoload=$1 [R=301,L]
RewriteRule pencil2/painting/(.*)$ https://www.example.net/pencil1.php?autoload=$1 [R=301,L]
RewriteRule pencil3/painting/(.*)$ https://www.example.net/pencil2.php?autoload=$1 [R=301,L]
RewriteRule pencil4/painting/(.*)$ https://www.example.net/pencil2.php?autoload=$1 [R=301,L]
RewriteRule pen1/painting/(.*)$ https://www.example.net/pen1.php?autoload=$1 [R=301,L]
# new style art pages without trailing slash
ReWriteRule watercolour1/(.*)$ https://www.example.net/watercolour1.php?autoload=$1 [R=301,L]
ReWriteRule watercolour2/(.*)$ https://www.example.net/watercolour2.php?autoload=$1 [R=301,L]
ReWriteRule watercolour3/(.*)$ https://www.example.net/watercolour3.php?autoload=$1 [R=301,L]
ReWriteRule watercolour4/(.*)$ https://www.example.net/watercolour4.php?autoload=$1 [R=301,L]
RewriteRule pencil1/(.*)$ https://www.example.net/pencil1.php?autoload=$1 [R=301,L]
RewriteRule pencil2/(.*)$ https://www.example.net/pencil2.php?autoload=$1 [R=301,L]
RewriteRule pencil3/(.*)$ https://www.example.net/pencil3.php?autoload=$1 [R=301,L]
RewriteRule pencil4/(.*)$ https://www.example.net/pencil4.php?autoload=$1 [R=301,L]
RewriteRule pen1/(.*)$ https://www.example.net/pen1.php?autoload=$1 [R=301,L]
RewriteRule pen2/(.*)$ https://www.example.net/pen2.php?autoload=$1 [R=301,L]
RewriteRule penandwash1/(.*)$ https://www.example.net/penandwash1.php?autoload=$1 [R=301,L]
RewriteRule penandwash2/(.*)$ https://www.example.net/penandwash2.php?autoload=$1 [R=301,L]
RewriteRule othermedia/(.*)$ https://www.example.net/othermedia.php?autoload=$1 [R=301,L]
# catch all
RewriteRule watercolours.php /watercolour4.php [R=301,L]
RewriteRule watercolours1.php /watercolour1.php [R=301,L]
RewriteRule watercolours2.php /watercolour1.php [R=301,L]
RewriteRule watercolours3.php /watercolour2.php [R=301,L]
RewriteRule watercolours4.php /watercolour2.php [R=301,L]
RewriteRule watercolours5.php /watercolour3.php [R=301,L]
RewriteRule watercolours6.php /watercolour3.php [R=301,L]
RewriteRule watercolours7.php /watercolour4.php [R=301,L]
RewriteRule watercolours8.php /watercolour4.php [R=301,L]
# Block unwanted crawlers/spammers
order allow,deny
deny from 199.187.209.30
deny from 198.144.149.253
allow from all
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$ [NC,OR] Redirect permanent /fuchsia_cuttings.php https://www.example.net/fuchsia-cuttings RewriteRule ^fuchsia_cuttings\.php$ https://www.example.net/fuchsia-cuttings [R=301,L] The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
# added 21/3/17 to redirect all http calls to https
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L]
# Make clean url's
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/JWJforum/$
RewriteRule ^([A-Za-z0-9-]+)\/?$ $1.php [NC,L]
# to set the 404 page to a php version
ErrorDocument 404 default
ErrorDocument 404 /404.php
I have found that the problem is not related to the php extension specifically, but to the absence of any extension. In a url like example.net/folder/page.suffix, a typo in folder or page or suffix will return my custom 404 page, however, with no suffix the custom 404 doesn't get displayed. I've tested this with php pages and jpg images.
"/foo" - rewritten to "/foo.php" - results in "File not found."
"/foo.php" - not rewritten - results in "File not found."
"/foo." - not rewritten - results in custom 404.
"/foo.anything" - not rewritten - results in custom 404.
"/JWJforum/foo.php" - not rewritten ("/JWJforum/" is explicitly excluded by the condition) - results in "File not found."
"/JWJforum/foo.anything" - not rewritten - results custom 404.
"/foo" - not rewritten - results in custom 404 ....?
"/foo.php" - not rewritten - results in "File not found." ....?
"/JWJforum/foo.php" - not rewritten ("/JWJforum/" is explicitly excluded by the condition) - results in "File not found."
So what we're saying here is that this ISN'T an htaccess issue but a server issue that I need to take up with my hosting service.
is this issue a problem worth solving
# Rewrite direct requests for ".php" files that don't exist to "/404.php"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ 404.php [L]
# Append ".php" to single path segment URLs where the corresponding ".php" file exists
RewriteCond %{REQUEST_URI} !^/JWJforum/$
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([\w-]+)/?$ $1.php [L]
RewriteRule pencil1/painting/(.*)$ https://www.example.net/pencil1.php?autoload=$1 [R=301,L]
and so on and so on. This whole set of rules can be compressed into much smaller set, along the lines of RewriteRule ^pencil(\d+)/painting/(.*) https://www.example.net/pencil$1.php?autoload=$2
RewriteRule ^watercolours(\d+)/painting/(.*) https://www.example.net/watercolour$1.php?autoload=$2
but don’t quote me; that’s off the top of my head. Why the change from watercolours, plural, to watercolour, singular? If you hadn’t done that, the two could have been all one rule, with capture in the form (pencil\d+|watercolours\d+). RewriteRule \.php$ 404.php [L]You meant to say /404.php and /$1.php ;) (Yes, the RewriteBase directive will achieve the same end, but I think it’s safer to just put a / at the front of all rewrite targets.)
RewriteRule ^([\w-]+)/?$ $1.php [L]