M
M
Megumin2017-03-22 06:09:53
Yii
Megumin, 2017-03-22 06:09:53

Yii2 404 error when removing /web from url on localhost using .htaccess?

Hello. The problem is this: using .htaccess I want to make the url display host/site/index instead of host/web/site /index . There is already a topic on how to do this, here How to remove web/ from url in Yii2 basic using .htaccess and urlManager rules? However, when I create .htaccess I put in there:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

Then in the root folder I add the line 'baseUrl'=> '' , in request in config/web.php, then when I try to access the site, the browser displays the following:
Not Found
The requested URL /web/index.php was not found on this server.
Apache/2.4.23 (Win64) PHP/5.6.25 Server at localhost Port 80

I am using the WAMP package as a server. And in my opinion about 2 months ago I did the same operation and everything worked. I tried reinstalling WAMP but the result is still the same. I went further and threw the same project on free hosting on the Internet, and what's funny there everything works fine with these .htaccess, but on the local it's such a disaster. In general, I spent more than one day looking for a solution to this problem, gave up and came here to ask for help. I can't figure out where the dog is buried. I am noob. Help me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-03-22
@slo_nik

Good morning.
I do through two .htaccess
First, in web/.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

Second, at the root of the project
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
</IfModule>

<IfModule mod_rewrite.c>
   RewriteCond %{REQUEST_URI} ^/.*
   RewriteRule ^(.*)$ web/$1 [L]

   RewriteCond %{REQUEST_URI} !^/web/
   RewriteCond %{REQUEST_FILENAME} !-f [OR]
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^.*$ web/index.php
</IfModule>

I do not specify anything in baseUrl if the application is located at the root, that is, at site.com.
If the project is located in a subdirectory, for example yii2_test and the address is site.com/yii2_test , then the baseUrl specifies the name of the directory.
In urlManager, so that you can go to site.com without any web and site/index in the address bar, the following rule is created:
'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rule' => [
               '' => 'site/index',
              '<_a:[\w-]+>' => 'site/<_a> // для остальных страниц по умолчанию, about, contact
            ]
       ]

All of the above applies to basic, if you need to remove frontend and backend from the address in advanced (you can specify any name for backend), then there .htaccess, which is in the root of the application, will have a different content, nothing changes in web / .htaccess.

V
Viktor Taran, 2017-03-22
@shambler81

Do you have 1 url or do you need a regular expression to remove all web from the url?
If you need to do this for the entire site
THEN there are already more options, the simplest thing is just to do a "soft link" and that's it, then the site will physically lie there and there. But most likely it won't work.
if you need regular

RewriteCond %{REQUEST_URI} ^/dir1/(.*)$ 
# если строка начинается с /dir1/ 
RewriteRule ^(.*)$ /dir1%1 [R=301,L] 
#Перенаправить все урлы с начинающиеся с dir1 на dir2 с сохранением дальнейшей структуры URL

gave more detailed examples here.
https://klondike-studio.ru/blog/snipet-for-htaccess/ - here I wrote other examples in more detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question