Yii2: Hiding frontend/web and backend/web from UrlManger - php

I managed to hide frontend/web and backend/web/ using .htaccess file. But the urlManager unable to understand that. I'm trying to create url like site/index and it should be something like this: example.com/site/index but it creates example.com/frontend/web/site/index.
How do I solve this problem with UrlManager?
Update: .htaccess in root directory.
Options -Indexes
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} admin
RewriteRule .* backend/web/index.php [L]
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !admin
RewriteRule .* frontend/web/index.php [L]
</IfModule>
and here is the one I used in both frontend/web and backend/web:
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

I use this .htaccess config and it works fine for me. You need only one .htaccess file in your site root directory, and set frontend / backend configuration as described. Maybe this will work for you too.

Related

Removing 2 folders from URL and redirecting to page

I am creating a new web app and this is how my project folders look like:
So as you can see I have renamed that htaccess in the root folder because I am not using that one and I am using the one situated inside of the public folder. I am trying to change my URLs from this:
http://example.site/public/pages/about
to:
http://example.site/about
I was able to remove the .php from the file extensions but the code I added to remove the folders from the URL is not working.
Note: in the root folder (App/index.php) the index.php file simply has this:
<?php
header('Location: public/pages');
so I am just using this to send to the correct folder.
This is what I have so far:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /App/public/
RewriteRule ^pages/(.+)$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]
I have seen this question/answer from this url:
Remove / Rewrite 2 directory names from URL
but it is not working for me. Looking forward to anyone who can help, thanks.
Assuming ``htdocs/app/is yourDocumentRootforapp.blu` domain.
Inside htdocs/app/public/.htaccess you may use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if URL has /pages/ then remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pages/(.+)$ /$1 [L,R=301,NC]
# landing page for app subdomain to show index.php
RewriteRule ^$ pages/index.php [L]
# check & rewrite if a matching .php file exists in pages/ subdirectory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/pages/$1.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]
Besides this you will need to use this code in htdocs/app/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# send all the traffic to /public/
RewriteRule .* public/$0 [L]

Removing File URL with .htaccess

I am writing a program and run it locally with xampp. the full path to the program is http://192.168.101.103:7777/WeltesMaft/index.php
I am writing a .htaccess in C:\xampp\htdocs\WeltesMaft\
containing this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://192.168.101.103:7777/WeltesMaft/%1 [L]
but somehow my htaccess doesnt work. it still showing like this
http://192.168.101.103:7777/WeltesMart/_resources/main.php
which is in index.php i am including main.php. So when user enters index.php, it will redirect to main.php in an instant.
I want to hide it so that the url looks like this,
http://192.168.101.103:7777/WeltesMart/
Please help me what am i doing wrong here...
You can use this code also.....
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _resources/main.php/$1 [L,QSA]
</IfModule>
Change RewriteBase to your directory name and change the htaccess rule as following and try again:
RewriteEngine On
RewriteBase /WeltesMaft
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule / index.php [L]

Make HTACCESS to not be applied for some subfolders

I am using this htaccess for my application.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any URL I will type in my browser, if the respective file doesn't exist, it will open the index.php.
The problem is I have many applications like this installed on the same domain.
For example, the root contains an index.php file and a htaccess like above.
I have also, /store, which contains a index.php file and a htaccess like above, too, but when I access /store/something it opens the index.php of root (not the correct /store/index.php
How can I solve this problem (how to make the htaccess of root to not override the htaccess of /store)? Thanks!
You can have a skip directory rule to skip all the directories:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# skip index.php and some directories
RewriteRule ^(store/|site1/|site2/|index\.php$) - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

How to undo changes made to a .htaccess file

I am fiddling around building my own CMS and playing around with the .htaccess file.
I was looking around the web for a snippet of code which would add a trailing slash onto every HTTP request.
When I inserted the code I realised that it would also remove the name of the subdirectory in which I was working.
URLS:
So this is what I had: localhost/project/index.php
This is what I wanted: localhost/projects/index.php/
This is the result I got: localhost/index.php/
But the real problem is this:
I saw the outcome, and I wanted to revert to example 1 from example 3. But after I deleted the code in the .htaccess file, the changes remained, and I have no idea how to revert this.
How do I revert this rewrite rule?
This is my .htaccess before implementing the "trailing slash"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /tesla0.2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
This is my .htaccess after:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /tesla0.2/
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I assume that youre htaccess file is in the "projects" directory?
Try changing your rule to:
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
You'll also need to clear your browser's cache, since the redirect is 301 (permanent), the browser will cache the redirect and use it indefinitely, since it's a permanent redirect.

mod_rewrite: Redirect to cache or script

I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.
Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.
But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.
I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Thanks in advance :)
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC]
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]
#other existing rules go here

Categories