How to use Magento 2 with an openlitespeed (free) web server? - .htaccess

I'm trying to setup Magento 2.3.0 server with OpenLiteServer and keep on bumping weird Rewrite rule errors:
Here's the full list of errors.
The website is working, but CSS and JS cannot be loaded so there are lot's of 404s
I'm thinking that I may be doing something wrong, or that OpenLiteSpeed does not support all of those commands inside the .htaccess files.
The magento 2 installation is just a basic magento 2.3.0 composer installation without anything added to it, and thus is set to default mode.
The file permissions are looking good too, but note that files are missing from the pub/static/frontend/luma/en_US/ directory:
Any hints?
Thank you

Assuming that the website is under /magento2 you'll need to insert the rewrites on the virtual host, at the rewrites section, enable the rewrite module as well.
RewriteRule ^/magento2/pub/static/version.+?/(.+)$ /magento2/pub/static/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule /magento2/pub/static/.* /magento2/pub/static.php?resource=$0 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /magento2/index.php [L]

Related

Deny access to files inside folder but allow a REST API

I am using this .htaccess to simulate a folder full of files (a simple REST API):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule files/(.*)$ api.php?file=$1 [QSA,NC,L]
Let's call this folder magicfolder where I am using this .htaccess file.
In fact, there is only api.php that is creating every file that is requested.
So, an url like mysite.com/magicfolder/files/image.jpg is converted into mysite.com/magicfolder/api.php?file=image.jpg.
This is great. But I want also to add some rules that makes any real file that exists in magicfolder (logs and even api.php) to be inaccessible for users.
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Make sure your htaccess rules file is present inside magicfolder folder.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^files/(.*)/?$ api.php?file=$1 [QSA,NC,L]

How to change file extension sitemap.php to sitemap.xml using htaccess

If I write URL domain.com/something-any-word website is running properly. But I need a sitemap in XML format so I write PHP code in sitemap.php. I want in runtime sitemap.xml (ref. website is https://www.webslesson.info/2017/06/make-dynamic-xml-sitemap-in-php-script.html)
my current .htaccess file is working without sitemap.xml
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Just rewrite the request to that sitemap as you like:
RewriteEngine On
RewriteEngine ^/?sitemap\.xml$ /sitemap.php [END]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
If you want sitemap.php to runtime show sitemap.xml and you want yourdomain.com/something-without-ext write this code in .htacces file
RewriteEngine On
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^sitemap$ sitemap.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Redirecting all requests from a subdirectory to a specific file

I wanted to redirect all requests from a specific subdirectory (/portfolio/*) to a specific file in the root of my website (portfolio.php). This way I want to be able to create project-specific pages within the same file as where the overview is being created.
I tried the following, but it didn't work since it stopped images from loading (images were located in the /images/portfolio/* directory), and it also disables my custom ErrorDocument...
RewriteRule /portfolio/(.*) /$1 [PT]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /portfolio.php [L]
Anyone has an idea on how to fix this?
Try getting rid of the first rule and adding the pattern to your last rule:
RewriteRule ^portfolio.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^portfolio/(.+)$ /portfolio.php [L]
And for good measure, make sure multiviews is turned off:
Options -Multiviews

Moving codeigniter project to a new hosting provider - .htaccess and index.php

I just moved my functional codeigniter project to a new web hosting provider and am now having challenges removing the index.php from the URL using a standard .htaccess mod-rewrite. Here is the .htaccess that was working fine:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found this discussion but I do not have root access to the apache server to make the suggested configuration change.
Using the provided .htaccess file above,
Works fine: http://www.mysite.com/index.php/plans
Doesn't work: http://www.mysite.com/plans
Any suggestions are appreciated.
If you really have to accept both forms of URLs likewise then you need two RewriteRules. Once accepting variants with then 'index.php' part and one without.
Something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.php/(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Your question's not very clear, but a common problem with mod_rewrite rules like this is that some servers need the request to be passed as a query. I.e. you'd need to change your last line to:
RewriteRule ^(.*)$ index.php?/$1 [L]

Multiple sites from one FuelPHP installation

I would like to run multiple sites from one FuelPHP installation. The main driver behind this is the ability to share models and database configuration.
I've seen a couple of suggestions requiring changes to the FuelPHP directory structure, or through the use of modules.
I don't really want to mess with the directory structure.
I don't think I completely understand how modules can help me, but I think the resulting configuration would be more complex than I need.
The solution I've come up with involves a simple change to the .htaccess file located in /public. Whereby I modify the rewrite rule based on the host name. Each site's controllers and views sit in subdirectories of the main structure.
Original .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Multiple site .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} main.loc
RewriteRule ^(.*)$ index.php/main/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} admin.loc
RewriteRule ^(.*)$ index.php/admin/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} api.loc
RewriteRule ^(.*)$ index.php/api/$1 [L]
</IfModule>
My question is, is this a viable solution? Do you foresee any pitfalls that I might encounter down the line?
My working solution has been to create three FuelPHP applications side by side, then symlink key directories like core, some packages and obviously the model directory.
One gotcha was that my FTP client ignored the symlinks, so I had to recreate them on the staging server. This will not be an issue when I finally get around to automating deployment using SSH.

Resources