Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här inaktiverar du PHP i vissa directories i WordPress

Att skydda din WordPress-webbplats från hackare är av högsta prioritet. Ett effektivt sätt att förbättra säkerheten på våra webbplatser är att inaktivera PHP-körning i vissa mappar.

Hackare försöker ibland ladda upp skadliga filer som är skrivna i PHP-kod. Om dessa filer kan köras kan de äventyra säkerheten på din webbplats.

Genom att inte tillåta PHP-kod att exekvera i vissa kataloger kan du hindra dessa skadliga filer från att orsaka skada, även om de laddas upp till din server.

Den här artikeln visar hur du inaktiverar PHP-körning i WordPress. Vi förklarar hur du använder .htaccess-filen för att aktivera denna säkerhetsåtgärd och skydda din webbplats från potentiella attacker.

How to Disable PHP Execution in Certain WordPress Directories

Varför inaktivera PHP-exekvering i vissa WordPress Directories?

Som standard gör WordPress vissa directories skrivbara så att du och andra behöriga användare på din webbplats enkelt kan uploada themes, plugins, images och videoklipp till din webbplats.

Denna capability kan dock missbrukas om den hamnar i fel händer, t.ex. av hackare som kan använda den för att uploada filer med bakdörrsåtkomst eller skadlig kod till din WordPress website.

Dessa skadliga filer är ofta förklädda till WordPress core-filer. De är oftast skrivna i PHP och kan runna i bakgrunden för att få full tillgång till alla aspekter av din website.

Låter skrämmande, eller hur?

Oroa dig inte för det. Det finns en enkel fix för det. Inaktivera helt enkelt PHP-körning i vissa directories där du inte behöver det. Genom att göra det kommer inga PHP-filer att runna i dessa directories.

Låt oss ta en titt på hur du kan förbättra WordPress säkerhet genom att inaktivera PHP-körning med hjälp av .htaccess-filen.

Inaktivera exekvering av PHP i vissa WordPress Directories med hjälp av .htaccess-filen

De flesta webbplatser i WordPress har en .htaccess-fil i root foldern.

Den här kraftfulla konfigurationsfilen används för att skydda admin area med password, inaktivera browsing av directories, generera en sökmotorsoptimerad URL-struktur med mera.

Som standard är .htaccess-filen placerad i WordPress-webbplatsens rotmapp, men du kan också skapa och använda ytterligare .htaccess-filer i dina inre WordPress-kataloger.

För att skydda din webbplats från filer med bakdörrsåtkomst måste du skapa en .htaccess-fil och uploada den till din websites /wp-includes och /wp-content/uploads directories.

Skapa helt enkelt en new fil på din dator med hjälp av en textredigerare som Notepad på Windows eller TextEdit på Mac. Save filen som .htaccess och klistra in följande code snippet inuti den:

<Files *.php>
deny from all
</Files>

Save nu filen på din dator.

Därefter måste du uploada den här filen till /wp-includes och /wp-content/uploads folders på din WordPress webbhotell server.

Du kan uploada den med hjälp av en FTP-klient eller appen File Manager i cPanel-instrumentpanelen på ditt webbhotell account.

Clicking the Upload button in Bluehost file manager

När .htaccess-filen med koden ovan har addats kommer den att stoppa alla PHP-skript från att runna i dessa directories.

Kontrollera efter bakdörrar i WordPress med hjälp av Sucuri

Genom att använda detta .htaccess-trick kan du stärka din WordPress-säkerhet, men det kommer ej att fixa en WordPress site som redan har hackats.

Bakdörrar är skickligt förklädda och kan redan vara dolda i vanlig syn.

Om du vill kontrollera eventuella bakdörrar på din website, måste du aktivera Sucuri på din website.

Sucuri

Sucuri är det bästa WordPress säkerhetstillägget på marknaden. Det skannar din website efter möjliga hot, misstänkt kod, skadlig kod och sårbarheter.

Inga annonser blockerar också effektivt de flesta hackningsförsök från att ens nå din website genom att lägga till en firewall mellan din site och misstänkt trafik.

Viktigast av allt, om din WordPress webbplats blir hackad, kommer den att städa upp det åt dig. För att lära dig mer kan du läsa vår Sucuri review eftersom vi har använt deras tjänst i flera år.

Du kan lära dig mer i vår guide om hur du hittar och fixar bakdörrar i en hackad WordPress-webbplats.

Expertguider om hur man förbättrar säkerheten i WordPress

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du inaktiverar PHP-körning i vissa WordPress-kataloger för att stärka din webbplats säkerhet. Du kanske också vill lära dig några andra säkerhetstekniker. Här är några av våra bästa guider om hur du förbättrar WordPress-säkerheten:

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

40 kommentarerLämna ett svar

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Mrteesurez

    I see this as a great form of securing a website by disabling PHP execution in folders. According to this article, only two directories are secured, what of others ? Have they secured naturally or hackers don’t need it during the attempt ?

    • WPBeginner Support

      Other sections of your site’s files normally require stricter access or have php files that are required to work for your site to work.

      Administratör

  3. Jiří Vaněk

    Thanks for these safety tips. I have created an htaccess file and will upload it to FTP. I have a website on my own server, so the question of security is entirely up to me. Thanks for the next step in making my WordPress a little more secure again.

  4. Unarine Leo Netshifhefhe

    I also have this alert on my Updraft plugin where backups are not happening can this be due to htaccess?

    ”Backup directory could not be created…

    The folder exists, but your webserver does not have permission to write to it. You will need to consult with your web hosting provider to find out how to set permissions for a WordPress plugin to write to the directory. (wp-content/updraft)”

  5. Brian Prom

    FYI: you have a typo in your code snippet for the .htaccess snippet.

    Using your code snippet as is (without the closing /) breaks image loading.

    • WPBeginner Support

      Thank you for pointing that out, our code should be fixed :)

      Administratör

  6. Vitor Gonçlaves

    I’ve found some .php files in the uploads folder created by plugins. Can I assume this won’t cause a problem, or do I have to analyse each plugin individually?

    • WPBeginner Support

      If you reach out to your plugins they can let you know the specifics for those files.

      Administratör

  7. nirbo

    Thanks for the information

    • WPBeginner Support

      You’re welcome :)

      Administratör

  8. cliff denney

    thank you very much

    • WPBeginner Support

      You’re welcome :)

      Administratör

  9. Suman Samanta

    Great writing! You have a flair for informational writing. Your content has impressed me beyond words. I have a lot of admiration for your writing. Thank you for all your valuable input on this topic.

    • WPBeginner Support

      Thank you, glad you enjoy our writing :)

      Administratör

  10. Thato

    Guys i think i have messed up my htaccess file, my website is completely not displaying images

    • WPBeginner Support

      Hey Thato,

      You can download your .htaccess file to your computer as a backup and then delete it from your website. Go to WordPress admin area Settings » Permalinks and click on the save changes button. This should regenerate your .htaccess file.

      Administratör

  11. Shawn Rebelo

    Do not do wp-content.
    Do wp-content/uploads.

    And this:

    order allow,deny
    deny from all

    May very on servers.

  12. Hardik

    Does it affect the uploads file to upload on webpages?
    I found that after uploading this htaccess file to the folder many of images from many posts are not displaying.

  13. Chuck Cochems

    Yeah, denying access to php files in the includes directory breaks the site because including actually obeys .htaccess restrictions.

    But the restriction on the uploads directory is very smart, and this should be there .BY DEFAULT in the uploads directory, and there’s no good reason for it not to be present.

  14. Stan

    What’s the method for IIS servers?
    Thanks,

  15. KOnnie

    ZOMG! can’t you just disable write access to /wp-includes folder?
    Why fight with consequences when you can prevent the cause?

    • Jonathan Hodgson

      Wouldn’t this stop wordpresss being able to update the files in core updates?

  16. Jeff Wigal

    You can also put this in your Apache virtualhost, which will accomplish the same thing:

    Order allow,deny
    Deny from all

  17. anton

    how to implement this code if we have combination of lower case and upper case on file extention for example on.php on my website its work but it s not working if the file named with.PHp ,.PHP .PhP or combination of them,the backdoor script still executed

    Thank you

    • Timothée Moulin

      You can put this in your .htaccess file

      Order Deny,Allow
      Deny from All

  18. Shams

    Hi Syed,
    Thanks for such an informative post and in fact it provides a great solution for saving WordPress from hackers.

  19. Vladimir

    Hi!

    I followed all your instructions in this article, but Its not working…

    Thanks

  20. Aurélien Debord

    A so useful post with such good and quick tips.

    Thanks

  21. Ramon

    I created an .htaccess file in the wp-includes folder. Site looked oke but my WYSIWYG editor in the admin pages wasn’t working. Had to remove the .htaccess file again. (WP 3.9.1)

  22. Wes

    I also found my wp-includes folder full of php files and I can’t see how using that .htaccess file in there wouldn’t break something. I did use it in the uploads dir.

    • Editorial Staff

      It does break it sometimes (depending on the plugin you are using), but not all the time.

      Administratör

  23. Red

    forgive my bad english…
    i followed all your instructions in this article, but when i go my dashboard to add a newpost, my post section was messed up. … i suspect the .htaccess was the problem.
    when i deleted it, the post was fine.

    • Editorial Staff

      Which directory did you upload the .htaccess file that caused this issue?

      Administratör

  24. Chris

    I added the .htaccess file to my wp-includes and didn’t have any problems. Thanks a lot of the tips.

  25. Brad

    I tried this in my /wp-includes/ directory, which is full of php files. Of course I could no longer access the site. Did you really mean to include the includes directory for use with the .htaccess file?

    Did you maybe mean /wp-includes/images ?

    • Editorial Staff

      Nope. We meant /wp-includes/ folder. We have this on our wp-includes folder. If for some reason it is breaking your site, then delete the .htaccess file from your wp-includes folder.

      Administratör

      • Brad

        Strange, my wp-includes folder has over 90 php files in it. And it does break the site. I took it back out immediately.

        But I did put it in the /wp-content/uploads/ folder and its works just fine there. Thanks for responding

        • Alfred

          Putting an htaccess file denying access to php files in a directory full of php files does seem rather odd. I assume it’s because these files are normally only included, not executed directly. If that’s true, wouldn’t it be better to just deny access to the entire directory?

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.