.htaccess how to display long sub-directory urls as short urls in address bar? - apache

I've been trying to solve this problem for a couple of days now.
My root folder is https://www.example.com, I have many articles on it but in different folders and sub-folders for better organizing.
The problem is that I want links to look better and not like:
> https://www.example.com/reviews/review_1/index.html
/review_2/index.html
/review_3/index.html
or
https://www.example.com/articles/blog/content_1/index.html
/content_2/index.html
/content_3/index.html
but instead, I want all folders under "reviews and articles" to appear like root/dirs:
https://www.example.com/review_1/index.html
https://www.example.com/content_1/index.html
...so is it possible to exclude ../reviews/ or ../articles/blog/ folder somehow, but to have the same directory structure and also not to have duplicate content, indexes or duplicate DIRs?
EDIT / I found the solution:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^review_1/(.*) reviews/review_1/$1
This code will redirect from folder reviews/review_1/ to unexisting folder review_1 on root and copy all content from reviews/review_1/ to review_1.

just create an associative arrow specifying the mapping between url's and what they mean under the hood, for example I use in php like this-
$route = array(
array(
'url' => 'review_1/index.html',
'map_to' => '/articles/blog/reviews/index.html'
),
array(
)
);
or better version like implementing simple routing-
$route = array(
array(
'url' => '/review/',
'controller' => 'controller_class',
'action' => 'model_class'
),
array(
)
);
better proceed with second process..
do some research in implementing routing yourself or use basic framework.
Hope it helps.

Related

using yii rest api with default URL GET format rather than PATH format

I have a problem with a yii rest api. I configured it to work following the tutorial on the yii framework page, but after that i realised that my api works BUT NOT some big PORTIONS of my PAGE since it is based on the GET URL format rather than PATH which is required by the rest api.
So in my config/main.php i have the following setting
'urlManager' => array (
'urlFormat' => 'path',
'rules' => array (
'student/<id:\d+>/<title:.*?>' => 'student/view',
'students/<tag:.*?>' => 'student/index',
array (
'apistudent/register',
'pattern' => 'api/<model:\w+>',
'verb' => 'POST'
),
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
)
),
I also have a controller named ApiStudentController with a method called actionRegister().
So as already stated the api works normally but my page does not since i set the urlFormat to be 'path'.
The question is... how can i use the rest api but without the PATH url format and rather the default get url format (index.php?r=apistudent/register)?
I too faced the same problem in yii 1.x. I just need my API controller alone in old GET format rather than in PATH format (as i changed my websites URLs in PATH format). Finally i got it worked with a small hack in script file
$app = Yii::createWebApplication($env->configWeb); //store the app
//Change the UrlFormat for urlManager to get if a get request is given instead of a path format one.
if (isset($_GET['r'])) {
Yii::app()->urlManager->setUrlFormat('get');
}
$app->run(); //run the app
I dont know whether this solves your problem. But this can give you an idea. Happy Coding!

modify routing in url manager in yii

I have different modules folder like user, cms, importcsv etc.
Here, I want to modify the URL so that all module names are replaced with the string admin and remain same for the controller, action and hide parameter
For example:
user/profile/view corresponds to admin/profile/view
cms/site/setting corresponds to admin/site/setting
I have tried it like this, but it doesn't work:
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'admin/<controller:\w+>' => 'user/<controller>',
),
Do you have multiple controllers in each module? If not, maybe you should just use a single admin module. However, if you really need these things to be separate modules, then maybe you need that last route to look like:
'admin/<controller:\w+>/<action:\w+>' => 'user/<controller>/<action>',

Yii multiselect widget

I have the following code that produces a jquery ech multiselect menu widget in Yii. (Eric Hynds multiselect).
I have downloaded the extension and unzipped in extensions folder
and my folder structure is My_lokal_Yii/protected/extensions/EchMultiSelect/EchMultiSelect
this is my view file code..
<?php $this->widget('ext.EchMultiselect.EchMultiselect', array(
'model' => $model,
'dropDownAttribute' => 'color',
'data' => $list,
'dropDownHtmlOptions'=> array(
'style'=>'width:378px;',
),
));?>
and in config/main.php i have imported the extension as
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.EchMultiselect.*'
),
But i am getting error that "Alias "ext.EchMultiselect.EchMultiselect" is invalid. Make sure it points to an existing PHP file and the file is readable."
I have given all the permissions for the EchMultiselect file.
Please help me in this.. Thank you.. I tried a lot but did't get the exact result..
my folder structure is
My_lokal_Yii/protected/extensions/EchMultiSelect/EchMultiSelect
but in your config/main.php, there is:
... 'application.extensions.EchMultiselect.*'
------------------------------------^
It lacks the capital letter "s" and therefore the exact path/naming to/of your extension, I had the same "problem" with it ;-) , maybe it helps...
Greetings

Yii - Hiding module name in URL

My present URL structure is as below:
http://www.mydomain.com/module/controller/action
I need to hide the module section of the URL. Is there any way this can be done?
Thanks.
To point the URL http://www.mydomain.com/customer/login to the module,
in you config (protected/config/main.php) under urlManager:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'customer/login' => 'module/controller/action',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
To make any controller action go to module/controller/action (as discussed below) you can use:
'<controller:\w+>/<action:\w+>'=>'module/controller/action',
or
'<controller:\w+>/<action:\w+>'=>'module/<controller:\w+>/<action:\w+>',
Depending on whether the controller/action part of the value (on the right hand side of the =>) is a set value, or a variable.
So if you want any controller/action to go to the exact url module/controller/action, you would use the first example. For example if you want the controller/action site/test to go to module/controller/action, you would use the first example above
If you want any controller/action to go to a dynamic controller/action you use the second. For example, if you want the controller/action site/test to go to module/site/test, you would use the second example above
This new rule must be above the 3 default Yii rules as they are read and top to bottom and match the first rule it finds only
Yes, you can define any url rules in your config.
Your url rule may look something like this:
'<controller:(foo|bar)>/<action>' => 'module/<controller>/<action>',

Apache friendly urls

I've got a small CMS system written in PHP and running on Apache. The format of the URLs this CMS system uses/generates is:
/display.php?PageID=xxx where xxx is just some integer number. As you can see, those URLs are not very friendly, neither for users nor search engines.
I believe that using mod_rewrite (or something like that) and .htaccess files I should be able to configure Apache for URL-rewriting. I have searched for information about this before but I did not find any easy method to do this, it always involved messing with regular expressions, which I'm not very familiar with.
Since the website in question is really simple and small, just 5-10 different pages, I would really like to be able to just hard-code the configuration, without any special rules or regexps.
I'd just like to map a friendly URL to an actual URL, perhaps like this:
/about = /display.php?PageID=44
/products = /display.php?PageID=34
etc.
Is it possible to configure the mod_rewrite plugin in a basic way like this?
Could someone explain the easiest method to do this? Explain it to me as if I was a child :-)
Thanks in advance!
well putting something like
RewriteEngine on
RewriteRule ^about$ ./display.php?PageID=44
RewriteRule ^products$ ./display.php?PageID=34
in your .htaccess-file shouldn't be the big deal I think...
URL Rewriting for Beginners is my favorite intro article to this, it should cover what you're looking for. In fact, the first actual example where you write a .htaccess file is almost identical to what you want.
Another way is filter by a dynamic php file with a mapping for pages or a routing strategy like frameworks like drupal code igniter ....
and your URL will be like
my-pages/about.html -> display.php?PageID=44
my-pages/products.html -> display.php?PageID=34
and so on
Here a suggestion for .htaccess file and the filter the action with this strategy
--- .htaccess file ----
*RewriteEngine on
RewriteRule ^my-pages/(.).html$ MY-URL.php [QSA,L,E]
---------------- MY-URL.php ---------
<?php
$PREFIX = 'my-pages/'; //--- not used
$mapping=array(
'about' => 44,
'products' => 34
);
$pathinfo= pathinfo( $_SERVER['REQUEST_URI'] );
/* $pathinfo['dirname'] -> my-pages
$pathinfo['basename'] -> ???.html
$pathinfo['extension']-> .html
*/
$page = substr( $pathinfo['basename'] ,0,-5);
if( isset( $mapping[$page] ){
// ---- redirect or include
YUOR CODE HERE
}
else {
//--- error 404
YUOR CODE HERE
}
?>