Remove dir name from url with .htaccess mod rewrite - php

I'm working on a codeigniter project and i have the following url:
mysite/Project/frontend/tournaments/table
and i would like to turn it into:
mysite/Project/tournaments/table
What should i write in .htaccess??
This is what i already have:
RewriteEngine on
#Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Have you tried it?
RewriteRule ^mysite/Project/frontend/tournaments/table/([^/]*)$ mysite/Project/tournaments/table/$1
Simple Rewrite rule will solve your problem i think

So, this is how I would do it using CodeIgniter Routing feature:
Assuming Tournaments is your controller.
//Example: mysite/Project/tournaments/2332
// Will accepts numbers or letters
$route['Project/(:any)/(:any)'] = 'Project/frontend/$1/$2';
//Example: mysite/Project/tournaments/table
// Will accepts only letters.
$route['Project/([a-z_]+)/([a-z_]+)'] = 'Project/frontend/$1/$2';
You can add another route rule for admin URLs like the following:
//Example: Project/admin/users/add
// You can use either (:any) or ([a-z_]+)
$route['Project/admin/(:any)/(:any)'] = 'Project/admin/$1/$2';
I suggest you to read Routing documentation, it's a bit tediuos at the begining, but it will help you a lot!

Related

Route querystring URL to controller and action in CakePHP

I'm trying to route an old URL like
...file.php?a=login
to a CakePHP request like
/somecontrollerA/login/
but it seems that routing only supports the URL without parameters, and mod_rewrite does not work since CakePHP uses $_SERVER['REQUEST_URI'] and not $_SERVER['REDIRECT_URL'].
Is it possible do something like this?
I finnaly found a solution, not really good but worked for the purpose.
open /webroot/index.php
put the following code at start of file (thanks to AD7six)
if (isset($_SERVER['REDIRECT_URL'])) {
$_SERVER['REQUEST_URI'] = preg_replace("/^(.*?)\/webroot/", "$1", $_SERVER['REDIRECT_URL']);
}
then .htaccess file inside /webroot/ (lines 3 and 4)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^a=login$ [NC]
RewriteRule ^(.*)myfile\.php$ controllerA/login [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
now cake recognizes myproject/myfile.php?a=login as /controllerA/login

Mod rewrite and dynamic URLs

i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.
Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]
Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]

How to mask the URL with htaccess in zend framework 1.10

Hi I Have to create the URL something like "http://www.contract.com/user/profileview/index/MQ==" to "http://www.contract.com/profile/india/MQ==". We have tried the masking with codeignitor and succeeded. But coming to the Zend it is throwing the error "Invalid controller specified (india)".
We have rewrite the htaccess rules to Mask the URL in codeignitor. Same is applied here, but it is not working here. My htaccess code is
#php_value magic_quotes_gpc off
RewriteEngine on
Options -Indexes
Options FollowSymlinks
RewriteEngine On
RewriteBase /
#Admin
RewriteRule ^admin(.*)$ public_mvc/admin.php [L]
#RewriteRule ^profile/(.*)/(.*)/(.*)(/|)$ user/profileview/index/$2 [L,NC]
RewriteRule ^profile/(.*)/(.*)/(.*)(/|)$ user/profileview/index/$2 [L,NC]
#RewriteRule ^profile/(.*)/(.*)/(.*)/?$ user/profileview/index/$2 [L,NC,QSA]
# Also Tried Ones. Start
#RewriteRule ^profile/(.*)/(.*)/(.*)/$ /user/profileview/index/$1 [NC,L]
#RewriteRule ^profile/(.*)/(.*)/(.*)/?$ /user/profileview/index/$2 [NC,L]
#RewriteRule ^profile/(.*)/(.*)/(.*) /user/profileview/index/$2 [NC]
#RewriteRule ^/profile/(.*)/(.*)/(.*)/?$ /user/profileview/index/$2 [QSA]
# Also Tried Ones. End
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ public_mvc/index.php
Rewriterule ^$ /home [r=301,nc]
What is the wrong with this? We have tried so many types options. But all went in vain. You can see all those different tries in the code. Please help me this.
Thanks in advance.
"http://www.contract.com/profile/india/MQ=="
above url can be rewriter in htaccess
method 1;
RewriteRule ^profile/(.*)$ redirectedUrl.php [NC,L]
redirectedUrl.php as page, it should be placed in the root directory, it cant shows an error, regards changing the url in htaccess!!!
method 2;
handling on bootstapper.php for redirect into some controller with another action for using
Zend_Controller_Router_Route_Regex
Thanks for the Support. I have achieved the goal in some other way. We have changed the ZEND routing according to our needs. So we have Auth file(which is called in every request). This file is called in all the requests. So what I did is I checked the URL and if the URL contains the module name as 'Profile' then I have set the Module, Controller and Action name using the "setModuleName", "setControllerName", "setActionName", etc..
$ModuleName = $request->getModuleName();
$ControllderName = $request->getControllerName();
$ActionName = $request->getActionName();
/*
This condition is added to mask the URL. This is added by Ghanta Kiran on 27-Dec-2013. Start
Example : (From) http://www.contractskills.com/profile/india/username/id ==> (To) http://www.contractskills.com/user/profileview/index/id
*/
if($ModuleName == 'profile')
{
$paramId = $request->getParam('id'); // This is to get the User Id from the URL, to fetch the user profile details.
$request->setModuleName('user');
$request->setControllerName('profileview');
$request->setActionName('index');
$request->setParam('id',$paramId);
}
So what it will do is, If the URL Contains the 'Profile' in it, it will set the Module, Controller, Actions explicitly. I have done so many changes to the .htaccess, but ultimately all those efforts went wild.

Can't get mod_rewrite to work

I am trying to get url from:
192.168.0.1/movie-page.php?id=123
to:
192.168.0.1/movie/movie-name
or even (for now):
192.168.0.1/movie/123
I've simplified it by using this url (to get something working):
192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/
my .htaccess file:
RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]
What am I doing wrong? I've tried many combinations but no luck and my patience is running out...
I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.
If you want to rewrite:
192.168.0.1/movie-page.php?id=123 too
192.168.0.1/movie/movie-name or 192.168.0.1/movie/123
Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
So a better method is to route everything through the rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
Then handle the route by splitting the $_GET['route'] with explode()
<?php
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
//example
$route[0]; //movie
$route[1]; //movie-name
}
?>
You want something like this:
RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]
That will give the movie ID version with a numeric ID.

How to remove Controller and function name from URL in CodeIgniter

I am having a serious issue with one application developed in CI.
Currently my URLs look like this
http://www.example.com/content/index/mission/
I want to remove /content/index/ from URL So, It should look something like this.
http://www.example.com/mission
I have routing and .htaccess method as well. But nothing seems to be working.
Here is my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|css/js/style/system/feature_tab/robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
I have also tried routing of CI by defining reouters in config/router.php But it was not working :(
For your specific example, you'll want to create a route (in application/config/routes.php) that maps
$route['mission/']
to
"content/index/mission"
In other words, $route['mission/'] = "content/index/mission";
See the CI documentation regarding URI routing for more info
You can go into application/config/routes.php and set your own URL routing rules. (i.e. use something totally different than Controller/Funcction). There should be an array called $route which lets you assign mappings of url => controller/function. Hope this helps.
Check out this guide, its right up you're alley:
http://codeigniter.com/user_guide/general/routing.html

Categories