Yii2 advances app rest api pretty url not working - .htaccess

This is my folder structure
-api
-config
main.php
-web
.htaccess
-backend
-config
main.php
-web
.htaccess
-frontend
-config
main.php
-web
.htaccess
in my api/main.php I enabled pretty url. This is my url manager
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'user',
'site'
],
'extraPatterns' => [
'POST,OPTIONS search' => 'search',
'GET site/error'=>'error'
]
],
],
],
and I configured my api/web/.htaccess also
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
but for every url I am getting the default site/index result. When I remove pretty url code it is working also code it is working in my local,but not working in my server. Do I need to change anything in .htaccess ?
my yii-app url in server is like www.servername/my-yii-app/api/web. Any idea?

Related

Yii2 when urlManager is enabled i get 404 error

i created a project in Yii2.
When i enable urlManager in configs i get 404 error.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
in my htaccess i have
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
when i open url like this https://www.example.eu/yii/web/site/contact/
i get
The requested URL /yii/web/site/contact/ was not found on this server.
/yii/ is folder on server where is my project
without urlManager all working fine.
Maybe i forgot about something?

Yii2 Pretty Url don't work in Share Hosting (Basic Template)

I m trying to deploy a small project build with Yii2 (Basic Template) in a Share Hosting enviroment. Everything is working fine in local.
The Host server was like below in the root directory:
/
---logs
---index.html
After reading the doc of Yii2. I then delete the file index.html and upload my project folders into the root directory. It s now looking like this:
/
---logs
---assets
---commands
---config
---controllers
--- ...
---views
---web
--- ...
After trying the website i got a blank page. I then decided to create a htaccess file in the root of host server to point the web folder like here
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
After this i can get the index page very well with
www.example.com
and I get a blank page when i try to access for example (with url prettyUrl activated)
www.example.com/articles/list
But When I deactivate the prettyUrl, it s working like for example
www.example.com/web/index.php?r=articles/list
The .htaccess in the web folder is like this:
Options +FollowSymLinks
IndexIgnore */*
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
# Disable Directory Browsing
Options All -Indexes
and the url rules are set in the web.php file like below:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => '/',
'rules' => [
...
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
The main majority of the shared hosts disables some of the directives from the htaccess file to prevent some security issues. And if a restricted directive is found in your htaccess file you will get an 500 error page.
It's very likely that if you remove the Options +FollowSymLinks directive everything will work.
Additionally you can check out the apache error log for more information.

typo3: realurl extension- configuration issue, include path-fragment in url

Assume I have a website www.mywebsite.com. My typo3 v4.7 frontend is located at, say,
www.mywebsite.com/cms/index.php
, and the backend is located at www.mywebsite.com/cms/typo3/.
How do I configure the "friendly" urls, that the realurl extension generates, in a way that urls are mapped as follows:
www.mywebsite.com/cms/index.php?id=45
to
www.mywebsite.com/cms/something
I want to use the autoconfiguration feature of realurl.
This is how the autoconfig file looks like:
<?php
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']=array (
'_DEFAULT' =>
array (
'init' =>
array (
'enableCHashCache' => true,
'appendMissingSlash' => 'ifNotFile,redirect',
'adminJumpToBackend' => true,
'enableUrlDecodeCache' => true,
'enableUrlEncodeCache' => true,
'emptyUrlReturnValue' => '/cms/',
),
'pagePath' =>
array (
'type' => 'user',
'userFunc' => 'EXT:realurl/class.tx_realurl_advanced.php:&tx_realurl_advanced->main',
'spaceCharacter' => '-',
'languageGetVar' => 'L',
'rootpage_id' => '13',
),
'fileName' =>
array (
'defaultToHTMLsuffixOnPrev' => 0,
'acceptHTMLsuffix' => 1,
'index' =>
array (
'print' =>
array (
'keyValues' =>
array (
'type' => 98,
),
),
),
),
),
);
?>
I can't get it done. There are more virtual hosts on this website. I don't want to build a new dedicated virtual host just for this typ3 installation.
Currently, redirects do not work. There is a generic with a "Page ... was not found on this server." error-message issued by Apache.
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /cms/
RewriteRule ^/cms/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)(/.*)?$ - [L]
</IfModule>
Just a tip: instead using TYPO3 in subfolder it's much more comfortable to put it into subdomain like cms.mydomain.com. In such case you don't need an additional re-configuration, and all elements works as it was normal domain.
I think your problem has currently nothing to do with RealURL and its configuration. You have a problem with mod_rewrite configuration. Start with the original one that is shipped with TYPO3 because the bit you posted is missing crucial rules. Try to set the RewriteBase but if it doesn't work, disable it.
Note that it's actually the last rule (RewriteRule .* index.php [L]) that invokes TYPO3 and makes it handle the request, which apparently doesn't happen right now.
<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
# Change this path, if your TYPO3 installation is located in a subdirectory of the website root.
RewriteBase /cms/
# Rule for versioned static files, configured through:
# - $TYPO3_CONF_VARS['BE']['versionNumberInFilename']
# - $TYPO3_CONF_VARS['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
# Stop rewrite processing, if we are in the typo3/ directory.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
# Redirect http://example.com/typo3 to http://example.com/typo3/index_re.php and stop the rewrite processing.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/typo3$ /TYPO3root/typo3/index.php [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Main URL rewriting.
# For httpd.conf, use this line instead of the next one:
# RewriteRule .* /TYPO3root/index.php [L]
RewriteRule .* index.php [L]
</IfModule>

Yii: .htaccess and urlManager for separate backend and frontend

I'm having a hard time to configure my .htaccess and the urlManager in a Yii project to have the frontend in http://www.example.com and the backend in http://www.example.com/backend with the following folder structure. Any help is welcome. Thanks.
/assets
/backend
/controllers
/config
main.php
/models
/views
/common
/models
/protected
/controllers
/config
main.php
/models
/views
.htaccess
backend.php
index.php
Solution: after the great help of #bool.dev everything it's working, so I'm adding here every needed final file. In the frontend I'm using path format for the url and hiding the index.php
/backend/config/main.php
$backend=dirname(dirname(__FILE__));
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
...);
/protected/config/main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
.htaccess
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yii/example/
RewriteRule backend backend\.php [T=application/x-httpd-php]
# 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
</IfModule>
backend.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/backend/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
index.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
According to this wiki article by Qiang, you could make the following changes and it should work:
// backend.php:
require('path/to/yii.php');
Yii::createWebApplication('backend/config/main.php')->run();
Then in your backend's config (i.e backend/config/main.php):
$backend=dirname(dirname(__FILE__));
$frontend=dirname($backend);
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
'import' => array(
'backend.models.*',
),
// ... other configurations ...
);
But for this to work we need the main .htaccess to route example.com/backend to backend.php, which i haven't figured out yet.
Edit:Just figured out:
RewriteEngine On
RewriteBase /projectroot/
RewriteRule backend backend\.php [T=application/x-httpd-php]
The RewriteBase was important for me, as the backend.php was not found when i hadn't given the correct projectroot, basically it should be the directory where you have the entry script backend.php.

Remove "/index/" from URIs in Kohana 3

As of right now all of my controllers are mapped like this:
http://example.com/index/index
http://example.com/index/services
http://example.com/index/contact
What I want to do is change the configuration of the URI to look like this:
http://example.com/index
http://example.com/services
http://example.com/contact
Here is my .htaccess file
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
My bootstrap.php has the following:
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE
));
How can this be achieved?
Your .htaccess code looks fine. Check that your Apache configuration is fine:
Kohana URL rewriting
Also, you need to edit your application/bootstrap.php and setup the initialization variables there.
Kohana::init( array(
'base_url' => '/',
'index_file' => FALSE,
) );
You may want to check the user guide, there is a page just for this setup.
http://kohanaframework.org/3.0/guide/kohana/tutorials/clean-urls
I assume that you use action_index, action_services and action_contact inside Controller_Index.
If so, all you need to do is remove the controller section from routes (in your bootstrap.php)
Route::set('posttype', '<param>', array('action' => 'index|services|contact'))
->defaults(array(
'controller'=> 'index',
'action' => 'index'
));

Resources