Yii2 After configuring httpd.conf, still appending the web folder in url - apache

This is the set up of my httpd.conf to my yii application
Alias /attendance "C:/wamp/www/myproject/web"
<Directory "C:\wamp\www\myproject\web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
This works fine if I open up in browser
http://localhost/attendance/
but when I navigate to the page like about,contact,Login
it will use the myproject in the url like this
http://localhost/myproject /web/site/about
also as you can see that the web folder is appended in the url
how can I use my alias and remove the appended web in url

Set DocumentRoot to your project path "C:/wamp/www/myproject/web".

Related

Yii2 | Redirect to backend/frontend with htaccess

I am using Yii2 advanced template on localhost and I want to redirect the user to backend/web when the url is localhost/site/admin and redirect to frontend/web when the url is localhost/site using .htaccess file.
I enabled apache rewrite and I have tried these solutions
advance template Yii2 - redirect to frontend/web using htaccess
Yii2 .htaccess redirect to backend part
Redirecting to backend via htaccess Yii2
but in all cases I get 404 Error when the url is localhost/site/admin
Not Found
The requested URL /site/admin was not found on this server.
and when the url is localhost/site the content of the root directory of my site is displayed to me.
Now my .htaccess in the root of project is :
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
and in backend/web and frontend/web is:
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
Although I had enabled mod_rewrite through the terminal via the following command:
sudo a2enmod rewrite
But it was not enabled and I manually activated it from ‍/etc/apache2/apache2.conf.
use it without site
localhost/admin

Htaccess is matching directories when a file with the same name exists

In my Apache server, I have the following structure:
/www
.htaccess
api.php
index.php
My .htaccess is a very simple file that redirects all the traffic to index.php:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
And my index.php file just prints whatever the path is:
<?php print_r($_REQUEST['path']); ?>
So when I try to access to web.com/test, I get a web that outputs Array ( [path] => test ).
However, when I try to access web.com/index or web.com/api, it returns a 404 error. I have checked that this only happens when I try to access a route with the name of an existing file. How can I prevent this behaviour?
Turns out that I have MultiViews enabled in apache2.conf file. From the Apache docs:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name.
So I just had to add Options -MultiViews either to my apache2.conf (server-wise) or to the .htaccess of my project (project-wise) to prevent that behaviour.

Deploying vue js app and getting 404 error in routes

I am using a webpack simple but after deploying my app to an hosting server my index page works fine but other pages gives a 404 error .Please i dont know if anyone have any idea what is happening .The webpack simple only generate build js file for me and thats all, i dont get an index.htm file in my dist folderc
I am going to show you how to do it but before read this from vue.js docs
Step 1
Run the following command
npm run build
Step 2
Copy the dist folder and put it in a new folder
Step 3
In the new folder create a new file and name it .htaccess
Step 4
Inside .htaccess file insert the following code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Now you application is ready to deploy!You can even run it using apache on local machine.
Please again, read docs you can find more configurations there for nginx, node.js and learn more about all server configurations.
Additional info: I am using laragon + apache and after the step 4, I put the new folder inside www directory of Laragon, and serve it with Laragon. I guess you can do the same by using xampp or wamp by putting the new folder in docs directory.
That is due to the history push mode on your vue router.
If you use Apache, follow these steps, else you can read the vuejs doc https://router.vuejs.org/guide/essentials/history-mode.html:
If Apache version is above 2.2, you can use Fallback ressource instead of mod_rewrite in your apache config. It works for me.
In /etc/apache2/apache2.conf
<VirtualHost *:80>
ServerName YourServerName(like yourwebsite.com)
DocumentRoot /var/www/yourAppLocation/dist
<Directory "/var/www/yourAppLocation/dist">
FallbackResource /index.html
</Directory>
</VirtualHost>
Or you can use classical mod_rewrite
<VirtualHost *:80>
ServerName YourServerName(like yourwebsite.com)
DocumentRoot /var/www/yourAppLocation/dist
<Directory "/var/www/yourAppLocation/dist">
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
</Directory>
</VirtualHost>
You need to set up your hosting to route every 404 error to index.html and let vue handle the errors , which hosting are you using? if you cant set up your error pages you could remove mode:'history' mode from your router or use hash mode instead, here is the official docs to how to setup your host, as for your index.html you can find it in your project root not in dist folder

Apache rewrite with fallback

In our AngularJS project I have a set of HTML templates for reports that are currently part of the deployment but this means that whenever our client requests a change to one of the HTML templates I need to make update the complete application.
Is it possible to do the following for a request for these HTML templates:
Assume the request for assets/reports/report1.html.
Use the file <external-dir>/assets/reports/report1.html if that file exists. The external directory is not directly accessible from the internet.
Otherwise, use the initial request.
Most mod_rewrite solutions I could find seem to stop after the first rewrite.
Update: I've added the configuration changes below to the complete virtual host definition but the Alias seems to disrupt things:
<iFmODule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
Alias /advisor/report /var/www/html/report
Alias /report-templates /opt/reports
#<Directory /var/www/html/report>
RewriteEngine On
# Preventing direct access to /report-templates
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/?report-templates(/.*)?$ - [R=404,L]
# If the request was not already rewritten,
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# and the file do really exist in /advisor/report/assets/reports
RewriteCond %{REQUEST_FILENAME} -f
# rewrite the request from /advisor/report/assets/reports to /report-templates
RewriteRule ^/?advisor/report/assets/reports/(.*)$ /report-templates/$1 [L]
# If the file do not exist in /report-templates/
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite the request back
RewriteRule ^/?report-templates/(.*)$ /advisor/report/assets/reports/$1 [L]
#</Directory>
LogLevel warn rewrite:trace8
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
There are 2 AngularJS applications, the advisor portal located under /var/www/html/advisor and the reporting application located under /var/www/html/report. The latter is accessed through https:/example.com/advisor/report and is the reason for the Alias.
I changed the Alias to:
<Directory /var/www/html/advisor>
RewriteEngine On
RewriteRule ^/?advisor/report/(.*)$ /report/$1 [L]
</Directory>
This way the Alias should not interfere. After this I enabled the <Directory> directive for /var/www/html/report and removed the advisor/ part from the rewrite rules and conditions but I didn't see the rewrite happening in the logs.
I assume you have a directory called external-dir in your root (public_html) folder.
If you have full access to your server, this directory could be also somewhere else on the file system, but than you have to add a Alias in your Virtualhost configuration.
Alias "/external-dir" "/absolute/path/to/external-dir"
and the Apache Module mod_alias must be enabled:
sudo a2enmod alias
Now the main Part: This procedure will work for all existing files in the folder (or sub folder) /assets. First we check if the same file exist in /external-dir/assets and if so we serve it, if not we serve the original one. Its not possible to get a file from /external-dir/assets that do not exits in /assets. If you need more restrictions you can add them as your need it.
You can put this it in a .htaccess file in your root (public_html) folder or in your Virtualhost configuration, than I would use a Directory Direktive for your root directory
RewriteEngine On
# Preventing direct access to /external-dir
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/?external-dir(/.*)?$ - [R=404,L]
# If the request was not already rewritten,
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# and the file do really exist in /assets
RewriteCond %{REQUEST_FILENAME} -f
# rewrite the request from /assets to /external-dir/assets/
RewriteRule ^/?assets/(.*)$ /external-dir/assets/$1 [L]
# If the file do not exist in /external-dir/assets/
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite the request back
RewriteRule ^/?external-dir/assets/(.*)$ /assets/$1 [L]
I tested this positive on an Ubuntu 16.04.03 LTS Server with Apache 2.4.27.

How to configure .htaccess for using Yii2 with Opencart in subfolder

I have an issue with proper configuration .htaccess file for Yii2 basic application.
I want to use Yii2 framework with Opencart store which a have installed to the /shop subfolder.
According to Yii2 server setup guide, I need to set /web directory as a DocumentRoot. But then my /shop directory (which is located in a root of Yii2 installation) becomes non web-accessible.
So I decided to break the rules and set my Yii-root as DocumentRoot and redirect all of the requests to the /web dir with assistance of Apache's mod_rewrite.
Please help me to create valid .htaccess file for Yii-root folder (in Opencart folder I've put it's default .htaccess file with just changing RewriteBase to /shop. It seems work properly)
I need something like this:
Lets assume I set DocumentRoot /var/www, then:
http://sitename.com => /var/www/web/index.php
http://sitename.com/any-non-file-request => /var/www/web/index.php
http://sitename.com/favicon.ico => /var/www/web/favicon.ico
http://sitename.com/shop => /var/www/shop
Works now. .htaccess file in /var/www (root of Yii2 installation)
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/web
RewriteRule ^(.*)$ web/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^web/(.*)$ web/index.php [L]