Search code examples
.htaccessxampplocalhost

Why I can't access some pages or directories on my localhost XAMPP?


First of all, I already dug the internet but I couldn't find a solution.

So, Here's what I did to my website and my step-by-step questions:

  1. I have a project running in my XAMPP

(Edited localhost to 127.0.0.1:80)

(Edited htdocs to my workspace folder)

  1. I tried setting up an htaccess file for my website just to remove php and html extensions:

Here you can see it's content:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
  1. I also have Visual Studio's Live Server Plugin which generates random amount of port. (Might be interfering?)
  2. Sometimes I have access to index.php sometimes I don't.
  3. I don't have access to /icons/... directory all the time.

I really need a serious help right here or I might lose my temper losing my project :(

My best of appreciations to the solver in advance <3


Solution

  • That rewriting configuration you posted makes no sense. How should the rewriting engine decide which rule to use? Do you expect it to somehot magically guess ?

    Take a look at this instead and work out the difference:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]
    

    A better variant would be that:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [END]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_URI}.html [END]
    

    And a general remark: you should always prefer to implement such rules in the actual http server's host configuration. Distributed configuration files (".htaccess") come with a number of disadvantages, they should only be used if no other alternative exists (read: if you are using a cheap hosting provider and have no access to the real configuration).