1

I have seen so many posts about this so apologies for yet another. I have tried to read around how this works with little success: To start with here is my .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

The first sets http to https - it works so .htaccess active. The second is meant to remove the .php from the url - it doesn't.

My main question is how to remove .php from each URL. My second rewrite rule is meant to do this but it doesn't. Yes, it is copied from a previous question but I would really like to understand exactly what the syntax means so that I can understand why it is not working.

So my questions are:

  1. Can anyone point me to a decent explanation of the syntax for RewriteCond and RewriteRule? I have looked a number of docs including the Apache http://httpd.apache.org/docs/current/rewrite/ docs but I am no better off.
  2. And / or why does the .php removal not work?

A cheeky extra simple question ... do I need to repeat RewriteEngine On or do I just need it once?

1
  • what is your original url? Commented Sep 2, 2016 at 5:38

2 Answers 2

1

Have it like this (see comments above each rule):

RewriteEngine On

# redirect externally http -> https for non POST data
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# To externally redirect (change URL in browser) /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally (silent) rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

References

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

5 Comments

Thanks - and yes I understand better what is going on now. Your code works to some extent but it now appear to remove any POST or GET data too.
Modified it to ignore POST requests from redirects. And no it won't impact GET variables so /file.php?foo=bar will be redirected to /file?foo=bar thus removing .php only. Make sure to always completely clear browser cache when testing a rule change.
Quite brilliant. Thank you so much. I will sit down and work through the guides to try and fully understand what is going on here. Very grateful.
Interestingly, this works but not for pages opened in a new tab.
Page opening in new tab is purely client side handling, a web server just gets a URL from browser to act on and it doesn't (and cannot) act differently for URLs to be opened in a new tab.
1

Try this way

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

https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

The first line is the conditional statement, which says: if the requested file name eg the php document the user is looking for is not found, then move on to the second line. Visitors for example may be looking for site.com/hello, but that file will not be found because what they really want is site.com/hello.php, therefor the conditional statement will pass.

The second line is the action to do when to conditional is true. It uses a regular expression to target all files requested from your web server, it then adds the .php extension. The $1 is whatever file name the regular expression match giving your visitors hello.php when they visit site.com/hello

Ps. You only need to turn on Rewrite engine once at the beginning

3 Comments

Thanks - I read that one and have already tried it :) Like so many of these things it does't tell you what it means, it just says copy this. I want a decent explanation as to what all the chars mean in the rule. And no ... still not working.
Added an explanation for the rule
Thank you so much I'm beginning to understand. I now realise that here I am not removing the php from the url, but rewriting a url that does not have the php extension to have it. Tested it and it is working this way. So my question is remains if I put abc/efg.php into the browser can it display abc/efg. Or am I misundstanding again?

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.