I've been trying to figure out how to remove .php extension, I've searched everywhere and it seems most of the code are not working anymore. The code below is what I am using now to remove the .php extension but it is not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- f
RewriteRule ^([^\.]+)$ $1.php [NC]
I'm using XAMPP on Windows 10. I also tried many times to remove the .php extension but the result was the same. But after a long while finally, I got how we can do this. If you want the same results, just follow the steps given below...
To remove the .php extension from a PHP file, e.g. if you want to change mywebsite.com/about.php to mywebsite.com/about you have to add the following code inside your .htaccess file:
First of all, make sure you've created a new file named .htaccess and placed it at your root-level/root-directory where your index file is placed.
Now, open this (.htaccess) file with any editor of your choice, copy/paste the given code and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Now, if you (any user) will access mywebsite.com/about in the browser, the user will see the content of mywebsite.com/about.php page.
But still, if you (any user) will access the URL as mywebsite.com/about.php, this will not redirect the user to this mywebsite.com/about page. But will go to this mywebsite.com/about.php page which means the user can still visit mywebsite.com/about.php page. Don't worry about it. If you want to avoid this, you can simply follow the next step.
To avoid the above problem, now you need to add some more rules in the .htaccess file. For this, you've to replace your old code with this new one given below, save your file and check it out...
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
That's all and you're all set.
I hope this will fix everyone's problem.
Have a nice day :)
To remove the .php extension from a PHP file
for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Or
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
Or
see removing .php extension from URL
Also, make sure that you've mod_rewrite on.
Also see how to create .htacess file
I think you need to change internal website coding as well with htaccess code above and remove the file name URL extension from where it is mentioned in your web coding.
Eg:-
https://yoursite.com/page.html
changed to
https://yoursite.com/page/
htaccess code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Related
For example I want to change:
www.example.com/forum/thread?id=1&topic=hello
to
www.example.com/forum/thread/1/hello
I've looked around and modified my .htacess file to look like this to modify these URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]
I keep getting a 404 error saying that the file doesn't exist. I'm wondering if it's because I'm removing the .php from the file first using this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But then when I remove that rule and go to thread.php?id=1&topic=hello it just breaks everything and gives me masses of errors
In your question, you state that you want to change:
www.example.com/forum/thread?id=1&topic=hello to www.example.com/forum/thread/1/hello
However, that is backwards.
The address that you rewrite to needs to be the address that the server can interpret. So, www.example.com/forum/thread/1/hello should be the "friendly" address that the user enters and the rewrite should add the .php extension (although with a rewrite, this will not be visible to the user in the address bar)
Try this:
RewriteEngine on
RewriteRule ^/?thread/([0-9]+)/([0-9a-zA-Z_-]+) /thread.php?id=$1&topic=$2
The reason that you are probably getting errors going to thread.php is that you have a rule to remove the .php (which now the server will not be able to render the page). With the above rewrite rule, you will get to it by going to :
www.something.com/thread/1/hello
Your rule will never get applied because you are using RewriteCond %{REQUEST_FILENAME}\.php -f this condition checks your filesystem to see if /forum/thread/1/hello.php exists and if it doesn't then your rule is ignored. Simply comment out or remove that condition from your rule .
Try :
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]
I would like to use .htacces for hiding the .php extension in address bar, I found the solution HERE, but this does not work for me. My .htacces in root looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
If I click on a link, it shows *.com/stg/xy.php
I originally would like to show just *.com, but I read, that is not a good option because of visitors, so I would be satisfied with *.com/stg/xy/
FYI: I also have another .htacceses in subfolders because of pwd protection. Should I put the same code in all of them?
I also have *.pdf files to open, I want to hide the extension, or path here too.
Thanks,
D
I think, I will use different folders with index.php and I will embed every single pdf file and use the method, mentioned above. I find this the best option in my case, because non of the "normal" methods work for me!
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.
I setup .htaccess file as follows to make elegant url by hiding .php extension;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now all of my pages work other than following three urls/pages;
www.auroracs.lk/about
www.auroracs.lk/enquiry
www.auroracs.lk/locate
for above URLS I get following error. (You may check online by trying that URL right now)
..............................
Not Found. The requested URL /about/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
..............................
But if I rename about.php to about1.php then www.auroracs.lk/about1 url works. same happens to other two php files also. Can any one help why its not working only for these three pages. Is it because my .htaccess conflicting with some other file or any advice is appreciated.
Replace your rule by this more correct rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
The problem: Some html pages of php equivalents (apple.html, apple.php; orange.html, orange.php), but not all do (grapes.html).
The goal: If the php version exists, rewrite, otherwise keep it the html version.
I have:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ /$1.html [R]
Interesting issues:
If I don't put / in front of $1.php then I end up with: site.com/document/root/path (ie: site.com/home/user/www/file.php)
When calling the second RewriteRule, I get http://site.com/http:/site.com/page.html and it tells me there were too many redirects. Notice how there is only one / in the second http.
I've made some progress, I added RewriteBase / and removed the / before the $1, but I still get the too many redirects error (the web page at site.com/page.html has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer).
It seems like if I just rewrite html -> php -> html I get the same error. So it looks like the logic is working, but that sort of logic isn't allowed. I can't think of any other way I could see if a "php version" of a file exists? The only way I can think of is do something similar to:
RewriteCond ^(.*)\.html$ $1.php -f
RewriteRule ^(.*)\.html$ $1.php [R]
Unfortunately that doesn't quite work (I'm guessing because it has three segments on the condition line). I'm trying to get the filename without the extension, then say if filename.php is a file, rewrite page.html to page.php
you should be able to achieve that by using two conditions:
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [R,L]
The first condition checks if the filename ended with .html and the second uses the back reference %1 from the first condition to check if .php version exists.
Hope it helps. :)
I'm sorry to answer sooooo late but you will need to add the RewriteBase directive to make it works.
I had the same problem (with your http:/stufff) and fixed it this way :
RewriteBase /your/application_path
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [L]
Hope it will help !
I also wanted to Rewrite all .html request to .php files, but only if the .php file exist. But my variation was that this should only happen if the actual .html file does not exist.
So only calls to .html files that does not exist is Rewritten to .php file, if they do exists by these rules: (I also have tested this on a XAMP local server and also a Apache online server with success!)
# Rewrite rules to .html file to .php if the .php version
# does actually exist.
RewriteCond %{REQUEST_FILENAME} (.*)\.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %1\.php -f [NC]
RewriteRule ^(.*).html$ $1.php [NC]
It first check if the requested file is a .html file (we don't want .jpg, .css, .pdf, etc. to be rewritten).
The it checks if that .html file does not exist. (we don't want to rewrite to .php if it actually does exist.)
Then it checks if a .php version does exist (we don't want to rewrite to a non existing .php file).
Then it rewrites the .html to .php