Configure Yii2 Advanced on single domain with multiple backends - .htaccess

From: http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/#hh2
It works but I have created another backend.
This is the .htaccess configuration that I have:
RewriteEngine On
# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend2)/web/ - [L]
RewriteRule ^(frontend|backend)/web/ - [L]
# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^backend(/(.*))?$ backend/web/$2 [S=1]
RewriteRule ^backend2(/(.*))?$ backend2/web/$3 [S=2]
# handle the case of frontend
RewriteRule .* frontend/web/$0
# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.php
Final frontend URL: http:// example.com/ -> OK.
Final backend URL: http:// example.com/backend -> Error 404.
Final another backend URL: http:// example.com/backend2 -> OK.

You need to edit your .htaccess according to this:
RewriteEngine On
# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend|backend2)/web/ - [L]
# Handle the case of backend, skip ([S=2]) two following rules, if current matched
RewriteRule ^backend(/(.*))?$ backend/web/$2 [S=2]
# Handle the case of backend2, skip ([S=1]) the following rule, if current matched
RewriteRule ^backend2(/(.*))?$ backend2/web/$2 [S=1]
# handle the case of frontend
RewriteRule .* frontend/web/$0
# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.php
You need to edit backend2/config/main.php:
....
'components' => [
....
'request'=>[
'baseUrl'=>'/backend2',
],
'urlManager'=>[
'scriptUrl'=>'/backend2/index.php',
],
// use the following, if you want to enable speaking URL for the backend
// 'urlManager' => [
// 'enablePrettyUrl' => true,
// 'showScriptName' => false,
// ],
],
Also, you need to create environments/dev/backend2 and environments/prod/backend2 folders with appropriate content and fix all namespaces and paths to backend2.

Related

Codeigniter and .htaccess mod rewrite

I am having an issue with mod rewrite. I am also a total beginner to it. I am using CodeIgniter and trying to rewrite the following:
http://url.dev/news/news_selection?item=59
to look like this:
http://url.dev/news/news_selection/59/
I have followed a tutorial on: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ (more specifically the : "Patterns and Replacements" section)
The issue is I write my RewriteRule as below and it still does not rewrite the url after going to the page (eg. http://url.dev/news/news_selection?item=59)
RewriteRule ^news/news_selection/([0-9]+)/?$ news_selection?item=$1 [NC,L]
Any advice on where I am going wrong? Thank you.
I am not sure if the routes and controller affect this issue but I added them for reference below.
In my routes for codeIgniter I have
//_News Folder
$route['news/(:any)'] = 'pages/view2/_news/$1';
1: _news is the folder
2: news_selection is the php file
3: pages is the controller
My controller has the following function for view2:
public function view2($sub ='', $page='')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
$this->load->view('templates/footer', $data);
}
The rest of my .htaccess:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
You just need this to use url as above:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this for all controller. This file .htaccess is in application folder.
I figured it out after about 10 hours of trying everything today.
I decided that it may be a routing issue with CI and decided to try passing my variable from the url in the cleaned up format. So...
If I enter: http://url.dev/news/news_selection/59
It will work with the following route:
//_News Folder
$route['news/news_selection/(:num)'] = 'pages/view_test/_news/news_selection/$1';
and controller:
public function view_test($sub='', $page='', $var1='')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['item'] = $var1;
$this->load->view('templates/header');
$this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
$this->load->view('templates/footer');
}
By using these I can pass the page number as $var1 and then pass it on to my view via $data. Then I picked up $data['item'] as $item in my news_selection.php page and passed it to my model for processing. Reference to CI Manual for this.
After that the page processes like it normally would have.

Rewrite url to pretty after redirect to folder - htaccess laravel

Hello I have a "protected" folder on my server. In its .htaccess file for conditional redirect of some users I use the following rules:
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/special
RewriteRule ^$ /special [R,NE,NC]
In the /special folder I have a .htaccess file with the following rules:
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
RewriteRule ^(.*)$ / [R=301,NE,NC,L]
The application in the folder will be laravel based so my content will have to be served from index.php file residing in /special/laravel/public/index.php
I want the URL to look like /special/.
What rules should I put and where for this to happen?
This is a follow up to my previous question:
https://stackoverflow.com/questions/24487012/redirecting-specific-ip-to-special-content-htaccess-vs-php
Simply rewrite the URL with .htaccess: (goes in /)
DirectoryIndex index.php
#Redirect to /special/laravel/public if you haven't already and the IP is okay
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/special/laravel/public
RewriteRule ^(/special)(/laravel)?(.+) /special/laravel/public$2 [L]
#if IP does not match and you ARE in the folder, then Redirect to root
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} ^/special/laravel/public
RewriteRule .? / [R=301,L]
I think that'll work. I didn't test it though. I can add to it if you need me to. And of course for your use case you may need to add some more RewriteConds in there for validating the REMOTE_ADDR.
The way I handle it:
.htaccess:
RewriteCond %{REQUEST_URI} !^/load_page.php$
RewriteRule ^(.+)$ /load_page.php [QSA, L]
That causes the server to redirect internally to load_page.php if the requested URL is not load_page. Without the RewriteCond, I believe it would cause an infinite redirect. This should work, but I didn't test it because it's different from my implementation, since mine also handles rewriting URLS to have trailing slashes and never show .php which makes it a bit more complex and quite different.
load_page.php:
$SITE_ROOT = $_SERVER['DOCUMENT_ROOT'];
$URL = $_SERVER['REQUEST_URI'];
ob_start();
if (conditionOne){
//change $URL here if you need to or do nothing
} else if (conditionTwo){ //check the IP or whatever you want to here
if (aCondition){//if $URL starts with '/special'
$URL = str_ireplace('/special/','/special/laravel/public/',$URL,1);
}
if (is_dir($SITE_ROOT.$URL))$URL .='index.php';
}
if (!file_exists($SITE_ROOT.$URL))$URL = '/error/404.php';
include($SITE_ROOT.$URL);
$content = ob_get_clean();
echo $content;
It's something to that affect. The user doesn't see load_page.php and they don't see that you changed the URL to special/laravel/public, but you include the correct file.

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 and .htaccess file on production server

I am new to yii and have some problem in configuring .htaccess file on production server.
On localhost :
Location of Application : /www/connect_donors/
Default URL that yii provides is,
http://localhost/connect_donors/index.php?r=controllerId/functionName
We used the urlManager in /connect_donors/protected/config/main.php to configure the SEO friendly url's..
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
Now the URL that was working was
http://localhost/connect_donors/index.php/controllerId/functionName
Then I used the .htaccess file to remove index.php from the above URL.
Location of .htaccess is : /connect_donors/.htaccess
Following is .htaccess file,
RewriteEngine On
RewriteBase /connect_donors
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
URL Chenged to :
http://localhost/connect_donors/controllerId/functionName
Everything working fine and awesome.
But yesterday I uploaded the application on production server.
On Production Server
Everything remained same only I had to change the .htaccess file.
The .htaccess file on server is,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Now the following url:
http://donorsconnect.com/
loads the home page of server properly.
But,
http://donorsconnect.com/profile
redirects again to home page.
NOTE : There is no session set on the "profile" controller.
class ProfileController extends CController
{
public function actionIndex()
{
$this->render('index');
}
}
I tried lot of things, changing the .htaccess file to different codes. but none helped me.
Any help is appreciable.
Solution
I finally got the solution and the mistake I had done.
My Components had a request array containing baseUrl.
'components'=>array(
...
'request' => array(
'baseUrl' => 'http://donorsconnect.com',
...),
Due to this it was not loading. I did not find the real reason for that.
But after removing that 'request' array, its loading fine.
Check link,
http://donorsconnect.com/profile
You should not hide index.php like this, read carefully : http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
You should set showScriptName to false in your main config :
'urlManager'=>array(
.....
'showScriptName'=>false,
.....
),
And your .htaccess should look like this :
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

Remove index.php?route= from OpenCart

I'm a .NET developer but my friend called me for support.I made some changes with .htaccess but i'm corrupted SEO.
Everything started with "www" tag on url.I see we get some errors when we are not using "www" and i'm changed .htaccess.I'm added rewrite rule and redirected mysite.com to www.mysite.com.Our problems are solved but now we have another problem.
We are using Opencart - SEO and it's enabled.Our products seems like
http://www.mysite.com/epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html
when we are reaching with www.
But if i remove "www" tag on url, it seems like
http://www.mysite.com/index.php?_route_=epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html
and it corrupt SEO.
I want to see second url like first one.
I'm tried play with seo_url.php , .htaccess but it doesn't change anyting.
I'm also tried solution on Remove index.php?route=common/home from OpenCart but it's not worked for me.
Now my seo_url.php is default, i get back my changes.And my .htaccess is
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^route=common/home$
RewriteRule ^index\.php$ http://www.mysite.com? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteRule ^(.*)$ index.php?_route_=$1 [L]
Please help me before i get crazy.I spend 3 hours for that.
Thank you for all,
Greetings
Try This Its working for me using .htaccess.
RewriteCond %{THE_REQUEST} \ /index\.php\?_route_=?([^&\ ]*)
RewriteRule ^ /%1? [L,R]
Also have the same problem and any that I found not solved my problem. But! I remember that I also working with Laravel and in this engine well working url path. Don't ask why I associated OpenCart with Laravel. So I enable SEO radio button in System -> Settings -> Server you also can disable it I don't see difference in the site work.
This solution consist from several part:
1. In your root folder replace .htaccess file content with next shapshot:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>
2. Go to /system/library/url.php and find link method and replace it with next:
public function link($route, $args = '', $secure = false) {
if ($this->ssl && $secure) {
$url = $this->ssl . 'index.php?route=' . $route;
} else {
$url = $this->url . 'index.php?route=' . $route;
}
if ($args) {
if (is_array($args)) {
$url .= '&' . http_build_query($args);
} else {
$url .= str_replace('&', '&', '&' . ltrim($args, '&'));
}
}
foreach ($this->rewrite as $rewrite) {
$url = $rewrite->rewrite($url);
}
$url = str_replace('index.php?route=', '', $url);
return $url;
}
Since you changed .htaccess in root folder and change url generating method you also need remember about admin part. If you what see pretty url in admin just copy .htaccess from root folder to /admin folder.
If you want save orignal url in admin side you must little bit change link method with next:
public function link($route, $args = '', $secure = false) {
if ($this->ssl && $secure) {
$url = $this->ssl . 'index.php?route=' . $route;
} else {
$url = $this->url . 'index.php?route=' . $route;
}
if ($args) {
if (is_array($args)) {
$url .= '&' . http_build_query($args);
} else {
$url .= str_replace('&', '&', '&' . ltrim($args, '&'));
}
}
foreach ($this->rewrite as $rewrite) {
$url = $rewrite->rewrite($url);
}
// Skip admin path
if (strpos($_SERVER['REQUEST_URI'], '/admin') !== 0) {
$url = str_replace('index.php?route=', '', $url);
}
return $url;
}
Result:
Before
http://YOUR_SITE/index.php?route=product/product&product_id=52
After
http://YOUR_SITE/product/product&product_id=52
Before
http://YOUR_SITE/index.php?route=account/login
After
http://YOUR_SITE/account/login
Just try this..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Old question i know but maybe someone would find it helpful: The reason SEO urls break if you change the HTACCESS rule is that they depend on _route_ for clarification that "yes this is a request for a clean url, no its not a standard route".
If you change index.php?_route_= in the HTACCESS you must also change the _route_ IF conditionals in /catalog/controller/common/seo_url.php. It may get heavily wonked out though if you do this, try it on test stores first.
Nothing should be seeing the _route_ except you in the logs or things like analytics logs since its a masked utility uri. If you see real humans are asking what the link is, then take action. Otherwise, its best to leave it.

Resources