I have found some roque URLs within Google Search Console which I am trying to remove. Somehow Google has found a number of URLs with a .php file extension.
Within Stackoverflow I found this similar question : URL Extensions removal
I added the following lines of code to my .htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I was hoping that if a browser requests a url with the file extension intact, it would re-direct to one without.
I don't think it is the above code that is failing, more likely a setting on my server - what other reasons would make this code not do as intended ?
https://www.martincorby.co.uk/test.php
https://www.martincorby.co.uk/test
My Answer:
There could be a few reasons why the code in your .htaccess file is not working as intended:
1. Make sure that the .htaccess file is located in the root directory of your website.
2. Check if the mod_rewrite module is enabled on your server. You can do this by checking the Apache configuration or contacting your hosting provider.
3. Verify that the AllowOverride directive is set to All in the Apache configuration file for the directory where your .htaccess file is located.
4. Check for any conflicting rules in other .htaccess files or server configuration files that may be overriding your rules.
5. Make sure that the file extensions you are trying to remove are actually being served by the server as PHP files. You can check this by accessing the URLs directly and seeing if they are processed as PHP files.
6. Clear your browser cache and try accessing the URLs again to see if the redirection is working.
If you have checked all of the above and the issue still persists, you may need to consult with your hosting provider or a developer for further assistance.
Rate this post
3 of 5 based on 6161 votesComments