1

I have removed the '.php' file extension the URL in the .htaccess file, however, it seems to prevent my php code from running.

Could someone please suggest a way that I can get this to work?

RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)index[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
cass-dev27
  • 13
  • 4
  • You only took care of one half: redirection requests _with_ file name extension to a shorter URL without. You need to add the internal request which takes care to correctly handling incoming requests to those shortened URLs. – arkascha Jan 15 '23 at 10:29
  • 1
    You will find _many_ examples for that here on SO. – arkascha Jan 15 '23 at 10:29
  • Does this answer your question? [Redirect all to index.php using htaccess](https://stackoverflow.com/questions/18406156/redirect-all-to-index-php-using-htaccess) – zanderwar Jan 15 '23 at 10:43
  • Perhaps not, that's slightly more advanced than what you're trying to do. You need to take calls and redirect them to a PHP script still, you can't just remove the .php and think it'll work – zanderwar Jan 15 '23 at 10:46
  • You should be removing the file extensions in your HTML source, not `.htaccess`? Have you done that already? (The directives to remove the file extensions in `.htaccess` is only for inbound links and search engines that might have indexed the old URLs.) – MrWhite Jan 15 '23 at 11:47
  • You also need to explain in natural language more precisely what you are trying to do. The directives you have do a lot more than what you are stating. Is that intentional? That 3rd rule is quite specific and rather unique to your site (unless it's not actually doing what you think it's doing)? What requests are you expecting that to handle? – MrWhite Jan 15 '23 at 12:16
  • 1
    Thanks for this...managed to get it to work using the method below. I misunderstood it slightly, however, you I removed the file extensions in the HTML source as you mentioned. Thanks for your help! – cass-dev27 Jan 15 '23 at 13:12

1 Answers1

1

Try this:

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

This will internally redirect:

https://yourwebsite.com/hello -> hello.php
https://yourwebsite.com/world -> world.php

But it will only redirect if the PHP file exists.

zanderwar
  • 3,440
  • 3
  • 28
  • 46
  • 1
    Just tried this but still no joy...the PHP file does exist but it's giving me a '500 Internal Server Error'. Thanks for your help though, it's much appreciated! – cass-dev27 Jan 15 '23 at 11:22