Kohana 3: Hide part of url - .htaccess

I have a problem with hiding part of url in Kohana 3.
There are controlers stored in admin subfolder.
These controlers power admin panel of site.
So when I type :
http://mysite.xyz/admin
it works ok. I need only admin panel of that kohana project, and I decided to create subdomain admin.
So now when I type:
http://admin.mysite.xyz/admin
it works ok.
But I want to hide that admin part of url and when I will type:
http://admin.mysite.xyz
I will not load kohana site, but admin panel.
Here is my .htaccess:
RewriteEngine On
RewriteBase /
###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*)index.php/(.*)$ /$1$2 [R=301,L,NE]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|media)
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?kohana_uri=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?admin.schoener-tauchen.pl$
RewriteRule ^(/)?$ admin [L]
RewriteRule ^admin/(.+)$ $1 [L,NC,R]
It doesn't work. It gives 404 error.
Can someone help, please?
PS. Changing Kohana routing is unfortunately very complicated in this case. Htaccess changes seems to be easier.
EDIT
bootstrap.php route:
Route::set('admin', 'admin(/<controller>(,<action>(,<id>)(,<id2>)(,<id3>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(,<action>(,<id>)))', array('controller'=>'\w+','controller'=>'\w+', 'action'=>'\w+', 'param' => '.+'))
->defaults(array(
'directory' => 'admin',
'controller' => 'home',
'action' => 'index',
));

Use bootstrap.php
$is_admin = preg_match('/^admin\.schoener-tauchen\.pl$/', $_SERVER['HTTP_HOST']);
Route::set('default', '(<action>(/<id>))')
->defaults(array(
'controller' => $is_admin ? 'admin' : 'index',
'action' => 'index',
));
And remove from .htaccess
RewriteCond %{HTTP_HOST} ^(www.)?admin.schoener-tauchen.pl$
RewriteRule ^(/)?$ admin [L]
RewriteRule ^admin/(.+)$ $1 [L,NC,R]

I will suggest this solution, comment out both routes in bootstrap and replace them with this.
Kohana::init ( array (
'base_url' => '/'
));
Route::set('default', '(<controller>(,<action>(,<id>)(,<id2>)(,<id3>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'home',
'action' => 'index',
));
Here is Kohana official route guide

Ok. I solved it by the hack in .htaccess:
RewriteRule ^admin/(.+)$ $1 [L,NC,R]
In view's files and controllers there are links, URLs which contain that admin. Htaccess force it to skip that admin part.

Related

Removing a portion of the URL in Yii 2.0

If I enter the URL:
http://localhost/third/web/calculator
it just works fine. I now want to remove the web portion from the URL. I currently do not have .htaccess. And if I enter http://localhost/third/calculator it shows object not found.
I added this .htaccess in the root folder of the project:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/third/(assets|css)
RewriteRule ^assets/(.*)$ web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteCond %{REQUEST_URI} !^/third/web/(assets|css)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ web/index.php
Now if I enter http://localhost/third/calculator it shows the congratulations page of yii2, instead of the calculator page. If click in any other tabs like login with link <span class="glyphicon glyphicon-log-in"></span> Login the url shows the web still.
First you change config->web.php
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
]
Add .htaccess file in your project root directory
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(/.+)?$ web/$1 [L,PT]
RewriteRule ^(.+)?$ web/$1
Also you have to add .htaccess file in your_project->web directory
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

Redirect domain in .htaccess to yii application

i need redirect domain in .htaccess in root on server.
My .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/yii/projectName/
RewriteRule ^(.*)$ /yii/projectName/$1 [L]
Home page application works, but url manager doesn't work, it always redirects to home page.
My urlManager:
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
Thank you for your help
Try 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
I found this alternative solution a year ago here: http://www.yiiframework.com/wiki/214/url-hide-index-php/
EDIT:
URL manager settings seems to be OK, but be sure to add these rules AFTER your custom url rules:
'rules'=>array(
// ... add you custom url rules here, if there are any
// Home page
'/' => 'info/index', // edit this to your home page controller/action
// Default patterns for any other controller/actions.
// These are checked last, if there were no applicable custom rules for a specific URL
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/*' => '<controller>/<action>'
),

Codeigniter Wildcard Subdomain

How can I make a wildcard subdomain with codeigniter? I have try anything on google and this site. But it wont work, if not 500 error page it will return nothing (I mean no effect) because I got this on my .htaccess before :
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
I think it always conflict with that one above, btw what I desired to be is :
example.com/page => example.com/page (without subdomain will just be normal, with no www and no index.php)
abc.example.com => example.com/abc-channel
exc.example.com => example.com/exc-channel
abc.example.com/my/foo/page => example.com/abc-channel/my/foo/page
All will generated with no redirect.

RewriteRule for sitemaps in Zend Framework

I have the following .htaccess rules:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^sitemap_(.*)\.xml$ /xml/sitemap/$1 #[L]
RewriteRule ^.*$ /index.php [NC,L]
Unfortunately I can not use internal Zend Framework routing for processing sitemaps, but the rules above do not work.
Sitemap URLs look like:
/sitemap_index.xml
/sitemap_pages.xml
/sitemap_news.xml
Required rewrite works fine in cases I set it R=301 redirect or comment out the last rule. Both of these are not options. Can anyone help please?
Can't say about .htaccess. May be easier to do by zend router. Put this in the bootstrap:
public function _initRouter() {
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route_Regex(
'sitemap_(.*)\.xml',
array(
'controller' => 'xml',
'action' => 'sitemap'
),
array(
1 => 'sitemap'
)
);
$router->addRoute('sitemap', $route);
}

301 Redirect Help with Dynamic URL's and Multiple Id's

I just did a redesign for www.wildchildclothes.com which has a new URL structure. The old site ran on Zen Cart and had a bad URL structure. Here are some examples of what I want to redirect from:
OLD: www.wildchildclothes.com/page.html?chapter=1&id=21
NEW: www.wildchildclothes.com
and...
OLD: www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=13
NEW: www.wildchildclothes.com/customer-service.html
Here is what's in my .htaccess to achieve this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=13(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com/customer-service.html? [L,R=301]
But this doesn't work at all - I only get sent to my 404 page instead of begin redirected. Can anyone shed some light? The full .htaccess is posted below.
Thanks much,
Jonah
None of the above solutions worked for me, but this does:
To redirect www.wildchildclothes.com/page.html?chapter=1&id=21 to www.wildchildclothes.com:
RewriteCond %{query_string} chapter=1&id=21
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]
To redirect www.wildchildclothes.com/index.php?main_page=document_product_info&products_id=280 to www.wildchildclothes.com:
RewriteCond %{query_string} main_page=document_product_info&products_id=280
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]
To redirect www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=9 to www.wildchildclothes.com/customer-service.html:
RewriteCond %{query_string} main_page=faq_info&fcPath=0&faqs_id=9
RewriteRule (.*) http://www.wildchildclothes.com/customer-service.html? [R=301,L]
I'm sure there are better ways to do this but this works for me and .htaccess just gives me a headache so I'm not digging any further.
I hope this helps someone else out there!
Jonah
It would be easier and far better if you use PHP for the redirect. You could, for example, rewrite such requests to a PHP script that analyzes the URL arguments and redirects to the correct page. Maybe something like this:
// old-to-new.php
$rules = array(
array(
array('chapter' => '0', 'id' => '23'), // old URL parameters
'/customer-service.html' // new URL
),
// …
);
foreach ($rules as $rule) {
if (array_intersect_assoc($rule[0], $_GET) == $rule[0]) {
header('Location: http://example.com'.$rule[1], true, 301);
exit;
}
}
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;
And the corresponding RewriteRule:
RewriteRule ^page\.html$ old-to-new.php [L]
You want to allow more than one character in the query parameters. Change [^&]& to [^&]*&:
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html$ http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=12(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com? [L,R=301]

Resources