Yii2 | Redirect to backend/frontend with htaccess - apache

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

Related

yii2 rest api url rewriting (hide directories)

I would like to use clean urls on my yii2 rest api.
I'm having clean urls for my frontend application but I'm failing to create clean urls for my yii2 rest api application.
I created the rest api by following the tutorial (Yii2: RESTful api: tutorial). The rest api uses a module for versioning.
Two .htaccess files have been created.
One file in my root and an another one in my "api/web" directory.
root .htaccess file
RewriteEngine on
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteCond %{HTTP_HOST} ^api.localhost [NC]
RewriteRule ^(.*)$ api/web/$1 [L,PT]
'api/web' .htaccess file
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?r=$1 [L,QSA]
Browsing to 'http://api.localhost.com/v1/music' gives a 404 page not found error.
While 'http://api.localhost.com/api/web/v1/music' returns results
(I would like to hide 'api/web/' in my url)
'request' => [
'baseUrl' => str_replace('/api/web', '', (new \yii\web\Request())->getBaseUrl()),
],
Add this request component config in your main api config
Configure the host file to look in /project_folder/api/web

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

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".

apache redirect 301 to .php file extension

for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)
we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the ".php" extension.
different url works
abc -> abc2.php
same url doesnt work
abc -> abc.php
here is our code sample:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L]
# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php
# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php
we host with 1&1 shared host server.
php version is 5.5
I also tried this code that redirects but the page not loading now:
RewriteRule ^(.+)\.(.*)$ http://abc.co.uk/$1.php [R,NC]
also noticed url without file extension (.*|html) will not redirect
Try this .htaccess in DocumentRoot:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co\.uk$ [NC]
RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url's that did not require the .php extension on the URL.
Basically we're trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)
I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301's work on any other linux server that I try.

.htaccess mod_rewrite dynamic match with Yii generated urls

I'm trying to use Apache's mod_rewrite module to make dynamically generated urls from a Yii app, which always contain index.php as the router/bootstrap, into static urls through .htaccess available to browse through a web browser.
Example: localhost/projectname/index.php/commentfeed.xml is the dynamically generated page, but I want it to be able to be accessed through localhost/projectname/commentfeed.xml
Apache version I'm running is: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.0 through XAMPP for my development platform. The mod_rewrite module is present and loaded in Apache's httpd.conf file.
My .htaccess in localhost/projectname/ is as follows:
#Turning on the rewrite engine is necessary for the following rules and features.
#FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.so>
Options +FollowSymlinks
RewriteEngine On
#Unless an explicit file or directory exists,
#redirect all request to Yii entry script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Now, when I try to access commentfeed.xml through my browser without index.php (localhost/projectname/commentfeed.xml) I get a 404 error - Object not found!.
I've looked over the following guides & SO questions: URL Rewriting Guide, mod_rewrite documentation, and SO: Tips for debugging .htaccess rewrite rules but none of them had explicitly what I was looking for. Also, my apache error log is not reporting anything about the .htaccess file. Any help or guidance towards the correct usage would be greatly appreciated.
You need to tell the Yii UrlManager that the script name is not required. In your config/main.php file you should have:
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
),
),
Make sure you also have a route defined to specify the controller/action that generates the commentfeed.xml file.
More info: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
The specific answer to my problem lay in using <IfModule> ... </IfModule>. With my setup of XAMPP v3.2.1 and the apache server installed with it, the only lines of code I needed in the .htaccess was simply:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

why apache server redicect /doc to usr/share/doc

I have the rewrite ruls in .htaccess file under /var/www in my ubuntu10.4 server like below, When the url appears to be http://127.0.0.1/doc/view, the webpage shows "The requested URL /doc/view was not found on this server", and then I check the apach log, it logs "File does not exist: /usr/share/doc/view", so apprently apache redirect the /doc/view to /usr/share/doc/view. I don't know what it is going on here, can anybody help? thanks for help.
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
Apache's default config on many distributions makes it so you can go to http://localhost/doc/to read the Apache documentation. You can edit this out in your httpd.conf or apache2.conf file if you need to use /doc for your own URLs.