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
Related
I would like to make the URLs of my Store URL-friendly.
Current URL Structure
https://my-domain.com/store/store.php?page=packages&id=1
Desired URL Structure
https://my-domain.com/store/packages/1
And also for direct access to the PHP files such as:
https://my-domain.com/store/profile.php to https://my-domain.com/store/profile
How would I need to go after this? I really appreciate any help you can provide.
Also might be note worthy that in the base directory a WordPress site is running with its own .htaccess file.
I already tried it with this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/store/page/(.*)/id/(.*) /store/store.php?page=$1&id=$2
RewriteRule ^store/store/page/(.*)/id/(.*)/ /store/store.php?page=$1&id=$2
But that didn't work
This code will work.
RewriteEngine will remove .php from all PHP Files
RewriteRule will rewrite url like page/id
For Removing .php extension
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
For page/id
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)? store.php?page=$1&id=$2 [L]
</IfModule>
You can use this for the first part:
RewriteRule ^store/((?!store)[^/]+)/([^/]+)$ /store/store.php?page=$1&id=$2 [L]
Although nothing is wrong with anyone else's answers, the more modern way to do this (including WordPress, Symfony and Laravel) is to send non-existent URLs to a single router script. By doing this, you only have to mess with an htaccess file once to set things up, and never touch it again if you add more "sub-folders", you can do all of that in just PHP. This is also more portable which means you can bring it to other server platforms such as Nginx with little changes, and don't need to deal with RegEx.
The htaccess is fairly straightforward. Route all requests that start with /store/ and don't exist as a file (such as images, JS and CSS) or directory to a single new file called router.php in your /store/ folder. This is an internal redirect, which means it isn't a 301 or 302.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/ /store/router.php [L]
Then in your new router.php file you can parse $_SERVER['REQUEST_URI'] to determine the URL that was actually requested, and you can even rebuild the global $_GET variable:
// Parse the originally requested URL into parts
$requestUrlParts = parse_url($_SERVER['REQUEST_URI']);
// Parse the query string into parts, erase the old global _GET array
parse_str($requestUrlParts['query'], $_GET);
// Handle
switch($requestUrlParts['path']){
case '/store/store.php';
include '/store/store.php';
exit;
// Custom 404 logic here
default:
http_response_code(404);
echo 'The page you are looking for cannot be found';
exit;
}
I'd also recommend putting the htaccess rule into the site root's htaccess folder, above WordPress's. There's nothing wrong with creating multiple files, this just keeps things in a central place and makes it easier (IMHO) to debug.
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]
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.
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.
Let's say I have the project folder as follows:
folder/models
folder/view
folder/controls
folder/public
folder/library
Now let's say that the site folder is folder/public/ and inside that folder there's just one file called index.php. This file handle all the site page request via the GET parameter index.php?page=user for example will call the user.php file of the application in another folder. The point is that I'd like that an URL such as:
www.site.com/index.php?page=user&id=1
became
www.site.com/user/id/1
How can I do that?
This was taken from CakePHP .htacess rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
It will render everything under your host
http://www.site.com/* --> http://www.site.com/index.php?url=*
from here your index.php could parse $_GET['url']
//e.g browser requests www.site.com/user/id/1
$url = $_GET['url']; // user/id/1
$params = explode("/",$url); // array(0=>"user",1=>"id",2=>"1")
RewriteRule ^user/id/([0-9]+)$ index.php?page=user&id=$1
But it sounds to me that you should use so called router, redirect all trafic to index.php...
http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/ (check out this link)
In your case there is no point in using /id/, but here you go:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/id/(.*) index.php?page=$1&id=$2
Or what's a way better approach:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
Then handle the request in your index.php file by checking $_SERVER['REQUEST_URI'] against the patter you dream up.
Without apache rewrite rule, a micro framework named Slim can makes routing and templating for your php project. You'll define your routes only in index.php file. Like ;
Slim::get('/', function () {
Slim::render('index.template');
});
You will be implementing what is called the Front Controller pattern. If you Google that you will find several php implementations. I thought this series on building your own php framework was good.
http://fuelyourcoding.com/php-frameworks-just-roll-your-own-part-1/
Are you using Apache as web server?
If yes you can use *mod_rewrite* to accomplish that.
I have not done this myself, so I can't give you detailed instructions, but searching with google, using a search string like "mod_rewrite examples" lands you a lot of seemingly good tutorials.