Yii URL Rewrite not working - yii

I am developing a Yii app using YiiBoilerPlate.
I am able to get to http://localhost/testapp/frontend/www/
when I click on login its giving me the message
The requested URL /testapp/frontend/www/site/login/ was not found on this server.
is there any config missing?
.htaccess has:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
frontend/config/main.php has:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlSuffix' => '/',
'rules' => $params['url.rules']
),
in the apache access logs..it has:
*127.0.0.1 - - [23/Apr/2013:18:04:24 -0500] "GET /testapp/frontend/www/site/login/ HTTP/1.1" 404 516 "http://localhost/testapp/frontend/www/" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0"*
Tried all relevent suggestions online and still didn't work. any ideas?

Seems you have loginRequire on the http://localhost/testapp/frontend/www/ and it redirect you to loginUrl which does not exists.
You can easily solve it by adding to config:
// ......
'components'=>array(
'user'=>array(
'loginUrl'=>array('user/login'), # path to login controller/action
),
),
Otherwise if you don't have login controller, error will raise with message:
The requested URL /testapp/frontend/www/user/login/ was not found on this server.

Related

SSL added to website correctly but says insecure for admin panel

I have 2 websites one in craft cms 2 and the other in craft cms 3.
Everything works fine except the control panel always shows HTTPS insecure, for both the sites.
Below is my .htacces file,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.co.in
RewriteRule (.*) https://www.domain.co.in/$1 [R=301,L]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin http://www.domain.co.in
Header set Access-Control-Allow-Credentials true
</IfModule>
General.php
define('URI_SCHEME', ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );
define('SITE_URL', URI_SCHEME . $_SERVER['SERVER_NAME'] . '/');
define('BASEPATH', realpath(CRAFT_BASE_PATH . '/../') . '/');
return array(
'*' => array(
'environmentVariables' => array(
'siteUrl' => SITE_URL,
'basePath' => BASEPATH
),
'cpTrigger' => 'manage',
'omitScriptNameInUrls' => TRUE,
'defaultTokenDuration' => 'P1W',
'sendPoweredByHeader' => FALSE,
'enableCsrfProtection' => TRUE,
'pageTrigger' => 'page/',
),
'prod' => array(
'allowAutoUpdates' => FALSE,
'env' => 'prod',
'siteUrl' => 'https://'.$_SERVER['HTTP_HOST']
),
'stage' => array(
'devMode' => FALSE,
'env' => 'stage',
'siteUrl' => 'https://'.$_SERVER['HTTP_HOST']
),
'local' => array(
'siteUrl' => 'http://'.$_SERVER['HTTP_HOST'],
'devMode' => TRUE,
'env' => 'local'
)
);
For others with the same issue, I managed to get this resolved by adding baseCpUrl in the general.php file
'baseCpUrl' => 'https://www.domain.co.in'
For more details visit https://craftcms.com/docs/3.x/config/config-settings.html#basecpurl

Rewrite two parameters without redirect

I have tried to follow: rewrite url using mod rewrite without redirection
Without success so far.
I have a domain with a directory like this:
domain.com/sub/
I want to allow these URL options to navigate each:
domain.com/sub => domain.com/sub/index.html?p1=&p2=&full=
domain.com/sub/ => domain.com/sub/index.html?p1=&p2=&full=
domain.com/sub/NUMBER => domain.com/sub/index.html?p1=NUMBER&p2=&full=
domain.com/sub/NUMBER/ => domain.com/sub/index.html?p1=NUMBER&p2=&full=
domain.com/sub/NUMBER1/NUMBER2 => domain.com/sub/index.html?p1=NUMBER1&p2=NUMBER2&full=
domain.com/sub/NUMBER1/NUMBER2/ => domain.com/sub/index.html?p1=NUMBER1&p2=NUMBER2&full=
domain.com/sub/NUMBER1/full => domain.com/sub/index.html?p1=NUMBER1&p2=&full=full
domain.com/sub/NUMBER1/full/ => domain.com/sub/index.html?p1=NUMBER1&p2=&full=full
/sub/.htaccess:
RewriteEngine On
RewriteRule ^([0-9]+)/full(/)*?$ index.html?p1=$1&p2=&full=full
RewriteRule ^([0-9]*)(/)*([0-9]*)(/)*?$ index.html?p1=$1&p2=$3&full=

url manager show 404 error on contact page

I'm trying to make my url look like this mysite.com/contact unfortunately it shows a 404 error on the contact page. the login page works tho. the file is located in protected/view/site/contact.php
'urlManager'=>array(
'urlFormat'=>'path',
//'urlSuffix'=>'.html',
'rules'=>array(
'<view:(about|terms|faq|privacy)>' => 'site/page',
'<action:(contact|login)>' => 'site/<action>',
'<action:(registration|create)>' => 'user/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
'showScriptName' => false,
),
make sure you have this in your SiteController.php
/**
* Displays the contact page
*/
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
$headers="From: $name <{$model->email}>\r\n".
"Reply-To: {$model->email}\r\n".
"MIME-Version: 1.0\r\n".
"Content-Type: text/plain; charset=UTF-8";
mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
I render mine like so
'contact' => array('site/contact'),
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
Create .htaccess in your project folder with above code

How to switch locales/languages for different users

I have designed a web application, It works for two different users say user1 and user2, and both of the users need the view in different languages.
I have studied about yii:t() but by that method we have to define language in main.config, which set the same language for both users.
How can I translate my view in different languages for both users?
I hope this can help you:
you need to edit urlmanager.php in your components, if there is no file, you need to create one.
Check this url: Multilingual
Thanks.
Put this in your SiteController.php:
public function actionChangeLocale($locale) {
// (OPTIONAL) if is registered user (not guest), save preferred locale in database
if (!Yii::app()->user->isGuest) {
// Update user settings
$uid = Yii::app()->user->id;
User::model()->updateByPk($uid, array('locale' => $locale));
}
// change locale
Yii::app()->user->setState('_locale', $locale);
// redirect to previous page, in the new locale
if(isset($_SERVER["HTTP_REFERER"]))
$referrer = $_SERVER["HTTP_REFERER"];
else
$referrer = Yii::app()->getBaseUrl(true) . '/';
$this->redirect($referrer);
}
Edit your main.php config url manager rules:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'lang/<id:\w+>' => 'site/changeLocale',
To change locale, create a link to point user to desired locale:
http://www.mysite.com/myapp/lang/en
http://www.mysite.com/myapp/lang/zh
http://www.mysite.com/myapp/lang/ja
http://www.mysite.com/myapp/lang/in
...
If you saved the logged-in user's preferred locale in database, add this to SiteController.php Login action:
$uid = Yii::app()->user->id;
$user = User::model()->findbypk($uid);
$userLocale = isset($user->locale) ? $model->locale : Yii::app()->language;
Yii::app()->user->setState('_locale', $userLocale);
Above usage is for those using htaccess rewrite. Make sure base .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] # Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Related Articles:
http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n
Related Modules:
http://www.yiiframework.com/extension/ei18n/

Zend Framework: get subdomain parameter from route

UPD:
Solved. The problem was because we're using nginx as a frontend. So nginx doesn't pass the HTTP_HOST to apache.
Hi there!
I'm having a problem with getting subdomain parameter in my base controller on a production server while on the localhost it's ok. other parameters from url like controller, action returned as they should.
this returns null on production:
$agencyName = (string) $this->_getParam('agency');
no changes made to .htaccess:
RewriteEngine On
RewriteRule ^main - [L,NC]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
and here's my vhost settings:
<VirtualHost *:8080>
ServerName agencies.domain.com
ServerAlias *.agencies.domain.com
ErrorLog /var/log/apache2/agencies.domain_errors.log
DocumentRoot /var/www/agencies.domain.com/public/
<Directory "/var/www/agencies.domain.com/public">
Options -Indexes FollowSymLinks Includes
DirectoryIndex index.shtml index.php
AllowOverride All
# Controls who can get stuff from this server.
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Does anybody knows why it happenes?
upd:
routers in Bootstrap
public function run()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$plainPathRoute = new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
)
);
$config = $this->getOptions();
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':agency.' . $config['siteUri'],
NULL,
array(
'agency' => '([a-z0-9]+)'
)
);
$router->addRoute('subdomain', $hostnameRoute->chain($plainPathRoute));
parent::run();
}
and yes, I do have $config['siteUri'] defined and i also tried using :agency.domain.com getting the same problem again
Use the following :
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoute()
{
$this->bootstrap('FrontController');
$router = $this->getResource('FrontController')->getRouter();
$router->removeDefaultRoutes();
$plainPathRoute = new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
)
);
$router->addRoute('default', $plainPathRoute);
$config = $this->getOptions();
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':agency.' . $config['siteUri'],
NULL,
array(
'agency' => '([a-z0-9]+)'
)
);
$router->addRoute('subdomain', $hostnameRoute->chain($plainPathRoute));
}
}
If you provide a valid subdomain (ie. only consisting of characters a-z0-9), it will be passed in agency, if not then agency will not be set. (At least it works for me using ZF 1.11.3 :p).
Solved. The problem was because we're using nginx as a frontend. So nginx doesn't pass the HTTP_HOST to apache.