Get rid of "web" in routes

Hi guys,

For whatever reason I cannot succeed with getting rid “web” from application address.
I would love using .htaccess to turn

http://mydomain/frontend/web

into

http://mydomain/frontend

Can you help, please?
There are Google resources that show how to get rid entire “frontend/web” from the address, but this is not my case.

Thanks ahead!

When you use the advanced Yii application template, you will need to set up two separate domains and set their web root to point to frontend/web and backend/web, respectively.

In other words : the root of the routes (the application root) is the web directory.

If you are not able to set up the root properly, then you will need to use a application template that allows you to use the root directory as web root…

2 Likes

I am sorry, but this is “You have a problem with Windows? - Reinstall Windows!” kind of answer.
I would recommend you to avoid this way of educating people in a future. Neither informative nor helpful.
There must be some way to rewrite.

Tank you anyway.

Am I supposed by be physic? :wink:

Unless you explain your particular setup, then I am assuming that you are “using the advanced application template wrong”.

Perhaps you’d want to use a modified advanced template instead? Or maybe use it as inspiration: http://demos.krajee.com/app-practical

It is both informative and helpful!
Perhaps not to you - who made the mistake of not giving enough details - but other people are going to find it very enlightening, especially when they come across it via the forum search function :slight_smile:

Hi, you could try this config for advanced template: https://github.com/mickgeek/yii2-advanced-one-domain-config
After that you get:
http://mydomain/ as frontend
http://mydomain/admin as backend

BTW are you using custom .htaccess file already ? I think that both the basic and the advanced yii2 templates do not have /web in url in default configuration. Post here your current .htaccess file (and your apache config if you have control over apache at hosting server)

I never had web in routes or url. First properly setup folder structure

As example:

apache2
htdocs
------ yii2  // under htcdocs
yii2up   // same level as apache2

And index.php

<?php

defined('DS') || define('DS', DIRECTORY_SEPARATOR);

// comment out the following two lines when deployed to production
//defined('YII_DEBUG') or define('YII_DEBUG', true);
//defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../../yii2up/vendor/autoload.php';     // point to correct path
require __DIR__ . '/../../yii2up/vendor/yiisoft/yii2/Yii.php';   // same here

$config = require __DIR__ . '/../../yii2up/config/web.php';

(new yii\web\Application($config))->run();

htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On
    RewriteBase /yii2/    # your correct folder


    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Well,

Can you sometimes catch the idea, that you approach EXISITING setup - a running application serving already 100.000 people and nobody is going to change it’s directory tree at this moment?

Then I would give up. I would be satisfied with the existing url `http://mydomain/frontend/web’. IMO, it’s not a good idea to change the url of an existing app once it has started to work as a production.

You’d better forget it.

Or if you really want to get rid of “web”, you should try to elaborate your current setup … the current directory structure and the httpd configuration. You have been neither informative nor helpful for us to understand your problem so far. :disappointed_relieved:

Alexandr - I only wanted to “free” the new-built RESTful API tree from this nasty “web” :slight_smile:

So I wanted to leave:

“fontend/web” and “backend/web” as they are, but implement “api” without this stupid “web” suffix :slight_smile:

Oh, what an honor … but I’m not @samdark. :joy::joy::joy:
And you haven’t answered my question. What is your directory structure and what is your current httpd configuration?

And, I’m afraid you can not change the url rule for the RESTful API while keeping the one for the ordinary web app … I’m not sure. But you have to give us more information, anyway.

Ha!

So I am sorry for calling you with a wrong name - excuse me, please!

And according configuration - the virtual server points to a single “top” directory where entire application is stored including vendor directory… Not good, but so it is…

I have just deleted the last post of yours. You should not expose all the things in public! You have to be cautious about the security of your web app.

What I wanted to know is only the current configuration of your Yii application.

OK, thank you for caustion but… What do you actually mean by “current configuration of my Yii application” ? Do you really need all configuration files - what for?

I don’t want to read all the configuration files with each and every line, but only the relevant information regarding your Yii app.

  1. Is your production web server a shared host or a virtual private server? In other words, do you have a privilege to create your own virtual host?
  2. If the former, where is your web root and what directory do you have there?
  3. If the latter, what do you have in your vhosts.conf or similar for your web application host?
  4. What do you have in your .htaccess file in your web root directory?
  5. What url rules do you have for your applicaiton?

OK,

This is a virtual private server with PHP FastCGI/FPM configuration. I do not direct access to the configuration - it is managed by an external company.

Consequently - a server “web root” is a directory where entire Yii advanced is stored (so the directory containing “backend”, “frontend”, “common”, “console”, “autorestapi”, etc.

Thiere is a .htaccess file:

#IndexIgnore /

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

# get rid of www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# block access to hidden directories
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule "(^|/)\." - [F]

# if request begins with /admin remove admin and ad /backend/web/
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/$1

# other requests add /frontend/web/$1
RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin|autorestapi/web)
RewriteRule (.*) /frontend/web/$1

# remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# if backend request
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php

# if frontend request
RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php

# if API request
RewriteCond %{REQUEST_URI} ^/autorestapi/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /autorestapi/web/index.php
Options -Indexes

<FilesMatch “(.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$”>
# block accesses to backup/source files
Order allow,deny
Deny from all
Satisfy All

Then, the frontend and backend URL rules are complex, but generally they are only used to setup modules.

1 Like
# if request begins with /admin remove admin and ad /backend/web/
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/$1

Is this working fine? If it is, then you could do just the same for “/autorestapi” and “/autorestapi/web”.