1

I want my urls to be extensionless, so no .php extension, I also want there not to be an opportunity to access the URL with a trailing slash.

The following removes php extension and thens redirects to the extensionless url if you try to access it with .php

I started writing a rule to stop you accessing with a / and redirect, but it does not work, any help?

#this removes php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]

# stops you accessing url with / **DOESNT WORK**
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$  /$1 [R=301,L]

1 Answer 1

2

You're looking at things backwards: the first rule you have doesn't "remove the php extension", it adds it to URLs that don't already have it (technically, any that don't contain a period).

I think you want something more like this:

RewriteEngine On
RewriteBase /

# Remove .php from any URLs that contain it, using an external 301 redirect
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)\.php$  $1  [NS,R=301,L]

# Now add it back internally
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)$  $1.php?no-redirect-loop  [NS,QSA]

Edit: While debugging another similar answer, I realized that the previous solution I posted here wasn't going to work in an .htaccess file. I've edited the example code above to use a rather ugly kluge for breaking redirect loops instead. A side effect of the kluge is that all scripts will see an extra empty URL parameter named no-redirect-loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.