Redirection using Routing not working in yii2 - yii

I have ProjectsController.php in which I have actionIndex
In my main.php file which is in frontend/config/main.php I have done routing as below.
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'projects'=>'projects/index',
],
],
]
Which means I am trying to redirect to index action in projects controller with url /projects. I am able to access in my localhost like localhost/myproject/projects, but whereas when I try to access the same in live like www.myproject/projects/ it is redirecting to home page and if I try to access like www.myproject/frontend/web/projects/index then I am able to redirect to the correct page.Not only this particular action and controller, all the other routing mentioned in main.php are redirecting to home page.
Below is my .htaccess file code in my root file '/'
# prevent directory listings
#Options -Indexes
#IndexIgnore */*
# follow symbolic links
#Options FollowSymlinks
RewriteEngine on
#RewriteCond %{REQUEST_URI} ^/common/css/
#RewriteRule ^common/css/(.*)$ common/css/$1 [L]
#RewriteCond %{REQUEST_URI} ^(?!.*/web/)
#RewriteCond %{REQUEST_URI} !^/web/
#RewriteRule .* backend/web/index.php [L]
#RewriteCond %{REQUEST_URI} ^(?!.*/web/)
#RewriteRule ^(.*)?$ backend/web/$1 [L,PT]
#RewriteCond %{REQUEST_URI} !^/frontend/web/
#RewriteRule backend/(.*)$ backend/web/index.php [L]
#RewriteRule ^frontend/(.*)$ frontend/web/$1 [L]
#RewriteBase /
RewriteRule ^common/(.*)$ common/$1 [QSA,L,NC]
RewriteRule ^frontend/images/(.*)$ frontend/images/$1 [QSA,L,NC]
RewriteRule ^(.*)$ frontend/web/index.php/$1 [QSA,L,NC]
#RewriteCond %{REQUEST_URI} ^frontend/(assets|css|js|images|themes)
#RewriteRule ^frontend/images/(.*)$ frontend/images/$1 [QSA,L,NC]
#RewriteCond %{REQUEST_URI} ^/frontend/web/projects/$
#RewriteRule frontend/projects/(.*)$ frontend/web/index.php/projects/$ [L]
#RewriteCond %{REQUEST_URI} ^(?!.*/themes/)
#RewriteCond %{REQUEST_URI} ^/(assets|js|images|themes|uploads)
#RewriteRule ^themes/(.*)$ backend/web/themes/$1 [L]
#RewriteRule ^images/(.*)$ backend/images/$1
#RewriteRule ^css/(.*)$ backend/web/css/$1 [L]
#RewriteRule ^(.*)?$ backend/themes?$ [L,PT]
#RewriteRule ^(.+)?$ frontend/web/$1
#
#RewriteCond %{REQUEST_URI} !^/backend/(assets|css|themes)/
#RewriteRule .* backend/themes/$1 [L]
#Options -Indexes
#RewriteEngine on
#
#RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
#RewriteCond %{REQUEST_URI} !index.php
#RewriteRule frontend/web(.*) frontend/web/index.php [L]
#RewriteCond %{REQUEST_URI} ^/(assets|css|js|images|themes)
#RewriteRule ^assets/(.*)$ backend/web/assets/$1 [L]
#RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
#RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
#RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]
#RewriteRule ^themes/(.*)$ frontend/web/themes/$1 [L]
Below is the .htaccess from /frontend/web/
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
Please help. I am unable to resolve this. I am actually not able to understand the cause.

You need to create two .htaccess. One in your project root folder and another in your frontend/web folder.
Please follow the steps of Anant Singh's answer of this question.
Yii2:-Pretty URL's are formed, but not working (says 404 “NOT FOUND”)
I hope It will help.

RewriteRule ^(.*)$ frontend/web/index.php/$1 [QSA,L,NC]
Change above rule in your root .htaccess to
RewriteRule ^(.*)$ frontend/web/$1 [L,NC]

Related

htaccess - redirect everything to index.php except a folder and specific files

I have been fighting with this for way too long. I have a my PHP site hosted with a .htaccess file for rewriting and redirecting. It has been working great so far. Now I simply want to add a subfolder, /pp that everything inside it does not get redirected or rewritten, basically it should not get touched by my .htaccess stuff.
Now when I go to mysite.com/pp/test.php (that file does exist), it redirects to mysite.com/index
Here is what I currently have:
RewriteEngine On
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/pp/.* [NC] // this line is not working for some reason
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Updated .htaccess:
RewriteEngine On
# skip /pp/* from all rules below
RewriteRule ^pp(/.*)?$ - [L,NC]
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Just below RewriteEngine On line add this to skip pp/ and everything under this folder:
RewriteEngine On
# skip /pp/* from all rules below
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+pp[/?\s] [NC]
RewriteRule ^ - [L,NC]
# rest of your rules go below this

Error in .htaccess file for removing index.php after upgrading to php v5.5

I am using codeigniter framework. I tried to remove index.php, it was success. For some reason I need to upgrade php version to v5.5. Then I copy pasted same code for .htaccess then its not working now. Throws an error like below:
.htaccess file has following code and its on root folder outside application.
<IfModule mysql_auth_module>
AuthType Basic
AuthName "MySQL Member Page"
Auth_MYSQLhost www.kaoscms.eo
## Auth_MYSQLusername grafikkaos_kaoscms
## Auth_MYSQLpassword gJcY3xju}:XP
## Auth_MYSQLgrp_table
Auth_MYSQLdatabase grafikkaos_kaoscms
Auth_MYSQLpwd_table ks_users
Auth_MYSQLuid_field username_usr
Auth_MYSQLpwd_field password_usr
## Auth_MYSQL_EncryptedPasswords off
require valid-user
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*)$ admin.php/$1 [L]
RewriteRule ^admin/$ admin.php [L]
RewriteRule ^admin$ admin.php [L]
#RewriteRule ^install/(.*)$ install.php/$1 [L]
#RewriteRule ^install/$ install.php [L]
#RewriteRule ^install$ install.php [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^admin(.*)$ admin.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^install(.*)$ install.php [L]
</IfModule>
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|content|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Try this,it will fix your issue.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]

Having problems rewriting a folder location with Mod Rewrite

I have the below code in my .htaccess file, this all works fine.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^(.*),(.*)$ $2.php?rewrite_params=$1&page_url=$2
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteRule ^stores/([0-9a-z.-]+)/?$ shop.php?shop_id=$1 [L,QSA,NC]
RewriteRule ^stores/([0-9a-z.-]+)/ajax_files/watch_item\.php$ ajax_files/watch_item.php [L,QSA,NC]
RewriteRule ^stores/([0-9a-z.-]+)/ajax_files/save_field\.php$ ajax_files/save_field.php [L,QSA,NC]
RewriteRule ^stores/([0-9a-z.-]+)/images/form-cb-icons\.png$ images/form-cb-icons.png [L,NC]
RewriteRule ^group-break/([0-9]+)/[0-9a-z.-]+/?$ group_break.php?cb_id=$1 [L,NC]
RewriteRule ^group-breaks/([a-z]+)-[a-z-]+/?$ group_breaks.php?tab=$1 [L,NC]
However I'm trying to add a new rule at the bottom which is this:
RewriteRule ^billing/client/login/?$ login.php [L,NC]
However this doesn't appear to work. Not sure if it matters or not but the billing directory also has a .htaccess file which may be overwriting it? The contents of that file are below:
<Files ~ "\.(pdt)$">
order deny,allow
deny from all
</Files>
# Protect against Clickjacking
#Header append X-Frame-Options "SAMEORIGIN"
RewriteEngine on
# Force HTTPS
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=307,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
RewriteCond %{REQUEST_URI} ^(.*)/install.php$
RewriteRule install.php %1/install/ [R=301,L]
I wanted to put my code in the root .htaccess file as the one inside the billing dir is for other software which may get overwritten.
Can this be done?
Yes your guess is right. If /billing/.htaccess exists with some rewrite rules then you need to add this rule in /billing/.htaccess file:
<Files ~ "\.(pdt)$">
order deny,allow
deny from all
</Files>
# Protect against Clickjacking
#Header append X-Frame-Options "SAMEORIGIN"
RewriteEngine on
# Force HTTPS
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=307,NE,L]
RewriteCond %{REQUEST_URI} ^(.*)/install.php$
RewriteRule install.php %1/install/ [R=301,L]
RewriteRule ^client/login/?$ /login.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L]

Rewrites for Magento to get clean urls

I want to rewrite old Magento urls like this, if possible:
www.domain.tld/index.php => www.domain.tld/
www.domain.tld/index.php/category/ => www.domain.tld/category/
www.domain.tld/index.php/category/subcategory/ => www.domain.tld/category/subcategory/
This is how my Magento .htaccess looks like right now (redirecting none of the above).
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
# REMOVE HOME REWRITE FROM MAGENTO
RewriteRule ^home/?$ /? [R=301,L,NC]
# ADD WWW TO NONE WWW FOR BOTH HTTPS AND NONE HTTPS
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#REDIRECT ALL .HTML FILES AND ALL .HTML/ FILES WITH TRAILING SLASH
RewriteRule ^google[0-9a-f]+.html$ - [L]
RewriteRule (.+)\.html$ /$1/ [L,R=301]
RewriteRule (.+)\.html\/$ /$1/ [L,R=301]
# ADD TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# TRAILING SLASH CHECK
RewriteCond %{REQUEST_URI} !(.*)/$
#ADD SLASH IF MISSING AND THEN REDIRECT
RewriteRule ^(.*)$ $1/ [L,R=301]
#CHECK IF REDIRECT POINTS TO A VALID FILE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#SEND TO INDEX.PHP FOR CLEAN URLS
#RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
#REWRITE EVERYTHING ELSE TO INDEX.PHP
RewriteRule .* index.php [L]
</IfModule>
A big thanks in advance,
In your apache configuration file. e.g. httpd.conf. Append below code with change "Directory" with your actual DocumentRoot for the VirtualHost.
<Directory /var/www/html/www>
AllowOverride All
</Directory>

.htaccess error on wallpaper website

I installed wallpaper script but it's giving this error on main page:
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
I think this is .htaccess file. I am pasting the .htaccess file below:
#php_flag display_errors On
#Options +FollowSymlinks
RewriteEngine Off
RewriteEngine On
#change / with the relative path if you install the script in a subdirectory
#RewriteBase /wallpaperscript.com/script/
#This is for redirecting links with any subdomain to no subdomain
#RewriteCond %{HTTP_HOST} ^(.+).wallpapers.net [NC]
#RewriteRule ^(.*)$ http://wallpapers.net/$1 [R=301,NC,L]
#This is for redirecting links without trailing / to links with trailing /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !(.*)\.(.+){3,4}
#RewriteCond %{REQUEST_URI} !(.*)/$
#RewriteRule ^(.*)$ /$1/ [L,R=301]
#RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)-desktop-wallpapers([^/]*)(/){0,1}([^/]*)$ $4-desktop-wallpapers$5$6$7
#RewriteRule ^([^/]*)/([^/]*)/([^/]*)-desktop-wallpapers([^/]*)(/){0,1}([^/]*)$ $3-desktop-wallpapers$4$5$6
#RewriteRule ^([^/]*)/([^/]*)-desktop-wallpapers([^/]*)(/){0,1}([^/]*)$ $2-desktop-wallpapers$3$4$5
#RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)-wallpapers([^/]*)(/){0,1}([^/]*)$ $4-wallpapers$5$6$7
#RewriteRule ^([^/]*)/([^/]*)/([^/]*)-wallpapers([^/]*)(/){0,1}([^/]*)$ $3-wallpapers$4$5$6
#RewriteRule ^([^/]*)/([^/]*)-wallpapers([^/]*)(/){0,1}([^/]*)$ $2-wallpapers$3$4$5
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}&resource=$1 [L]
Is there any problem with this? My site's URL is www.hotwallpapers.pk
Try to put: "RewriteBase /" under "RewriteEngine On" so it looks like this:
php_flag display_errors On
Options +FollowSymlinks
RewriteEngine Off
RewriteEngine On
RewriteBase /
change / with the relative path if you install the script in a subdirectory
RewriteBase /wallpaperscript.com/script/
This is for redirecting links with any subdomain to no subdomain
RewriteCond %{HTTP_HOST} ^(.+).wallpapers.net [NC]
RewriteRule ^(.*)$ http://wallpapers.net/$1 [R=301,NC,L]
This is for redirecting links without trailing / to links with trailing /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).(.+){3,4}
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteRule ^([^/])/([^/])/([^/])/([^/])-desktop-wallpapers([^/])(/){0,1}([^/])$ $4-desktop-wallpapers$5$6$7
RewriteRule ^([^/])/([^/])/([^/])-desktop-wallpapers([^/])(/){0,1}([^/]*)$ $3-desktop-wallpapers$4$5$6
RewriteRule ^([^/])/([^/])-desktop-wallpapers([^/])(/){0,1}([^/])$ $2-desktop-wallpapers$3$4$5
RewriteRule ^([^/])/([^/])/([^/])/([^/])-wallpapers([^/])(/){0,1}([^/])$ $4-wallpapers$5$6$7
RewriteRule ^([^/])/([^/])/([^/])-wallpapers([^/])(/){0,1}([^/]*)$ $3-wallpapers$4$5$6
RewriteRule ^([^/])/([^/])-wallpapers([^/])(/){0,1}([^/])$ $2-wallpapers$3$4$5
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}&resource=$1 [L]