Rogue URLs File Extension Removal - php

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

Related

Hiding true URL from website visitor

I have a php website. The various pages are distributed in various directories. Now, when I try to send the link to any page to a user, I have to put some URLs like "www.mydomain.com/collection/latest/requestpage.html" or "www.mydomain.com/api/get/someapi.php". But this URL is too complicated. I want the pages to be referred via the URLs like "www.mydomain.com/collection/requestpage" and "www.mydomain.com/someapi" and also this same address to be displayed in the url bar of browser.
Thus, loading "www.mydomain.com/collection/requestpage" takes the user to "www.mydomain.com/collection/latest/requestpage.html" and loading "www.mydomain.com/someapi" takes the user to "www.mydomain.com/api/get/someapi.php".
So,
1) I want the actual directory structure to be hidden from the URL,
2) I want to hide the file type extension of the page being loaded, and
3) Show this modified URL in the address bar of the browser when accessing the specified page.
I know this can be done using a .htaccess file but I have never written anything like this before. I have a .htaccess file on my directory that redirects any request coming to "mydomain.com" to "www.mydomain.com". I want to add some code to this htaccess file to do the tasks I mentioned. Please suggest what requires to be done.
Thanks in advance.
1) to remove the directories from the URL just add the following codes to your .htaccess file
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^FOLDER_NAME/ FOLDER_NAME%{REQUEST_URI} [L,NC]
Change FOLDER_NAME with your actual folder names
2) to remove extensions from the URL use following code
RewriteEngine on
RewriteBase /
for html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
for php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
EDIT: while working with POST requests you have to change the RewriteRule as following
RewriteRule ^ %1 [R=307,L]
3) You are done with what you wanted
Hope this helps.

Remove .php extension after moving site to WordPress

I'm moving an old site from flat PHP files over to a new WordPress installation and want to make sure all the old URLs redirect properly. For example,
Old url: /va/apply.php
should now go to:
New url: /veterans-affairs/apply
I've got /va redirecting to /veterans-affairs properly, but cannot get the .php stripped from the URL.
I'm not sure if these needs to all be done in one step? I've tried everything I can find online and made as many tweaks as my limited knowledge in .htaccess has allowed.
This is also on WordPress, so there may be something I did that was conflicting with the pretty permalinks stuff there.
This is some of the code that I've tried among many others.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
This should redirect the user to the non-PHP location, but I keep getting a 404. This must be a combination of my code and WordPress' pretty permalinks.
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\.php$ $1 [L,QSA]
I have just had a quick look through where you are at and this above might help out. Add it to the wordpress htaccess above all the entries there so it can change this first... HTH
OK, I've finally got this working correctly. Again, what I'm trying to solve is to get this URL:
/va/apply.php
to correctly redirect to the new WordPress URL,
/veterans-affairs/apply
What worked for me was:
# This will remove the .php extension if it is not a directory, the file does not exist and it's not a WordPress specific admin page
RewriteCond %{REQUEST_URI} !/wp-(content|admin|includes)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# The basic redirect for /va
Redirect /va /veterans-affairs
I think what was breaking it was this final line that you find in all the examples:
RewriteRule ^([^/.]+)$ $1.php [L]
I think this was trying to actually resolve the URL before WordPress could do what it needed to do.
I also found this page which proved insightful
Hide .php Extension, Set Directory Index, Eliminate Duplicate Content, etc.

why i cant use htaccess in xampp?

my name is mr.rico , i just build up my website for a anime call akifansubs.com.
my design and the program is not have any problem.... but....
where i try to use .htaccess file my xampp does'nt respons
ex
akifansubs/index.php become like this akifansubs/index.
and this the code for htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp
this is what i found a few hours ago
what should i do ? try to u make some change in xampp httpd con but it doesnt work :(
I know that this question was asked multiple times already and is answered, but I will give a little more comprehensive answer based on my experience.
Here is the .htaccess code snippet that will help you:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
I want to stress some important things here for everybody's reference:
This code snippet doesn't remove entry scripts from url (such as index.php used by many PHP frameworks)
It only removes .php extension, if you want to remove other extension as well (e.g. .html), copy and paste 3rd block and replace php with other extension.
Don't forget to also remove extension from anchors (links) href.

.htaccess to hide php extension preventing POST

I've been trying to hide the php extensions for my site. And I've been able to pull together a few Stackoverflow answers to do just that. However, I just have one last issue where when I POST, the resulting page is showing the .php extension. Can anybody help me on what I need to tweak with my htaccess file to not show .php completely?
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Here are the two resources I used from Stackoverflow:
How to hide .php extension in .htaccess
Website Form do not carry any value when .htaccess File Extension Remover Code is Used
Remove this:
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
It says:
Dear mod-rewrite
If the request method is POST, whatever the url is, would you please leave it
unchanged without doing any rewriting? And would you please not apply any other rules?
Sincerely yours,
etc. etc.

Remove file extention from urls

I have some urls in my website like this..
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more
Now I am trying to convert above urls to user friendly url
http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials
This is code in my .htaccess file that I am tried so far. But it doesn't work.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Anyone can help me to remove .php etension from my urls??
Thank you.
Get rid of the conditions you are using and instead use the below 2 lines in your htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^whatever/?$ whatever.php [NC]
This will work even if user types a forward slash after the name or not
If you want to make them dynamic, here's a good tutorial on that
for a proper implementation so as not cause confusion during deployment if you dont have access to the htaccess file of your server or if it is override. You can consider a library like toro-php http://toroweb.org/ for easy routing.

Categories