Yii2 Advanced Apache Rewrite - apache

I'm trying to setup Yii2 Advanced Template on my server the front-end works but the back-end has problems all the assets return a 404 Error .
This is my Apache 2 vhost :
DocumentRoot /var/www/.../frontend/web
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/.../frontend/web/>
Options Indexes +FollowSymLinks MultiViews
AllowOverride All
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/backend [NC]
RewriteRule . backend/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Order allow,deny
allow from all
</Directory>
The "..." are in place to make the path shorter, please ignore them .
Also the backend is a symlink
Any ideas ?
Best regards,
Paul.

OK, maybe this will help others too .
What I've done is I've declared an Alias and another Directory directive also an RewriteBase declaration is needed for this to work.
<Directory /var/www/.../www/frontend/web/>
Options Indexes +FollowSymLinks MultiViews
AllowOverride All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Order allow,deny
allow from all
</Directory>
Alias /backend /var/www/.../backend/web/
<Directory /var/www/.../backend/web/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
RewriteEngine on
RewriteBase /backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Order allow,deny
allow from all
</Directory>

Related

how to rewrite example.com/abc.html to example.com/abc

I am trying the below code but not working for me
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Simple solution, enable MultiViews in Apache.
MultiViews hides the file extension in the URL.
<Directory "/folder/subfolder/subfolder">
Options +MultiViews
</Directory>

I can not redirect with .htaccess file

I tried to redirect any thing the user write after domainname/ to the index.php file but it is not working unless i write domainname/index.php/sdsdsdsd => this works but i wanna it without typing index.php.
This is .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
I found the solution when i changed
<Directory />
AllowOverride None
Require all denied
</Directory>
To:
<Directory />
AllowOverride All
Require all denied
</Directory>

.htaccess, <Directory />, gives Internal Server Error, 500

Where and how to use <Directory /> in order to prevent files and folder access in the / . <Directory /> gives for me 500 Internal Server error.
.htaccess
#if i add the next 6 lines (10 lines), i am getting the error "Internal Server Error"
<Directory />
Order deny,allow
Deny from all
Options None
AllowOverride All
</Directory>
<Directory /web>
Order Allow,Deny
Allow from all
</Directory>
RewriteEngine On
RewriteBase /
Options -MultiViews
DirectoryIndex /web/index.php
RewriteCond %{HTTP_HOST} ^somedomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.somedomain\.com$
RewriteRule ^/?$ "https\:\/\/somedomain\.com" [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{ENV:REQUEST_FILENAME} !-d
RewriteCond %{ENV:REQUEST_FILENAME} !-f
RewriteCond %{ENV:REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /web/index\.php?url=$1 [QSA,L]
Directory location must contain full relative path including /var/www/ or whatever you have there.
Hope it helps!

Apache httpd block spam urls?

I am using Apache httpd. I enabled apache's rewrite module. I need to block few urls (referer spam). I have permission to edit httpd.conf file. Is below syntax correct to block multiple urls?
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
RewriteEngine on
RewriteCond %{HTTP_REFERER} example.com [NC]
RewriteCond %{HTTP_REFERER} sample.com [NC]
RewriteCond %{HTTP_REFERER} somexxx.com [NC]
RewriteRule .* - [F]
</Directory>
I'm not an expert but, I think that:
the . in the RewriteCond needs to be escaped
you need OR flags on all but the final RewriteCond
the L flag on the RewriteRule helps performance
if you use a capture group you'll also block subdomains
So, I think you'll want something like this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
RewriteEngine on
RewriteCond %{HTTP_REFERER} (example\.com) [NC,OR]
RewriteCond %{HTTP_REFERER} (sample\.com) [NC,OR]
RewriteCond %{HTTP_REFERER} (somexxx\.com) [NC]
RewriteRule .* - [F,L]
</Directory>

Apache subdirectory as main server

I have a VirtualServer at api.host.com and I need to access the same content at app.host.com/api.
In Apache a have the following rule in httpd.conf :
<VirtualHost app.host.com>
DocumentRoot "C:\webserver\public\webapp"
ServerName app.host.com
<Directory "C:\webserver\public\webapp">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
Alias /api/ "C:/webserver/public/api/"
<Directory "C:/webserver/public/api/">
Order allow,deny
Allow from all
Require all granted
Options Indexes FollowSymLinks
</Directory>
</VirtualHost>
And in C:/webserver/public/webapp, .htaccess is :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
RewriteRule ^/api/(.*)$ /api/index.php?_url=/$1 [QSA,L]
RewriteRule ^api/(.*)$ api/index.php?_url=/$1 [QSA,L]
I'm getting 404 not found when I access app.host.com/api/test but in app.host.com/api it is all ok.
In C:/webserver/public/api, .htaccess is :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
And works just fine with api.host.com/test.
You need to swap the order of your rules. Your first rule's ^(.*)$ regex is matching all of your requests, so you need your api stuff before that rule gets a chance to match the request. Something like this:
RewriteEngine On
RewriteRule ^/?api/(.*)$ /api/index.php?_url=/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
In httpd.conf, I changed the path to
Alias /**apicall**/ "C:/webserver/public/api/"
<Directory "C:/webserver/public/api/">
Order allow,deny
Allow from all
Require all granted
Options Indexes FollowSymLinks
</Directory>
And used Jon Lin hint, changing .htaccess rule to
RewriteRule ^api/?(.*)$ /apicall/index.php?_url=/$1 [QSA,L]