It happens that I need to fix a Codeigniter issue urgently while being in no way familiar with the tool.
Simple question: how do I allow links back from Facebook like http://www.xxx.de/?fb_action_ids=4811819099741&fb_action_types=og.likes&fb_source=timeline_og&action_object_map=%7B%224811819099741%22%3A447766801925104%7D&action_type_map=%7B%224811819099741%22%3A%22og.likes%22%7D&action_ref_map=%5B%5D
without creating either the infamous The URI you submitted has disallowed characters - which I can fix by setting $config['permitted_uri_chars'] - and neither a 404 because of the internal redirects of CI.
I'd love to learn of a quick fix for that issue.
If you can use .htaccess you can redirect to wherever. This avoids any CI intricacies.
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^ /index.php/? [L]
https://stackoverflow.com/a/4227616/183254
update: possible working rewrite for nginx
if ($args ~ fb_action_ids=(\d+)){
rewrite ^ http://example.com/ permanent;
}
Set these vars in application/config/config.php:
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE; // This is normally FALSE
Adjust $config['permitted_uri_chars'] if necessary. Mine are a-z 0-9~%.:_\-#! and work for almost everything.
I'm not sure what you need to do with the fb data, so all I can say is that with those options set, you should, in theory, be able to access the get vars supplied by facebook.
codeigniter expects the first part of the URI to be the controller it loads, then the second will be the method, and every segment after that are the parameters for the method that gets loaded. A fast solution is set up a route with a regular expression like you would in MVC3 and point it to a controller and method. You can then parse the uri from there.
Match ?fb_action_ids and change it to /controller/method/?fb_action_ids and go from there. Perhaps with a little more information I could help you even further but if this works for you this should be the quickest way to get up back up and running without having to reconfigure a bunch of things.
http://ellislab.com/codeigniter/user_guide/general/routing.html
Related
Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.
My .htaccess currently looks like this:
RedirectMatch 302 ^/\w{5}$ /redir.php
The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.
As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.
EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.
RewriteRule ^\w{5}$ /redir.php [R,L]
you can use the following rule:
RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]
this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:
$_GET['redir']
Edit: Or as #LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:
RewriteRule ^(\w{5})$ /redir.php [NC,L]
I am attempting to implement an oembed provider using the Silverstripe framework but have come across an issue.
I have a controller routed from the url /omebed.json and it works fine if I call something like /omebed.json?mediaurl=mymovie.mp4.
However the Oembed standard states it should be /omebed.json?url=mymovie.mp4
But Silverstripe internally checks the $_GET['url'] variable and will attempt to route to that page/controller.
So SilverStripe is trying to route to /mymovie.mp4 skipping my controller and hitting the ErrorPage_Controller creating a 404.
I'm thinking im going to have to extend the ErrorPage_Controller and rejig it if the url is oembed.json, but this seems a little hackish.
Any suggestions?
Cheers
Extending on #Stephen's answer, here is a way to get around that issue without duplicating main.php and without modifying it directly.
What I did was create a _ss_environment.php file which is added early on in the loading process of Silverstripe.
_ss_environment.php
global $url;
$url = $_GET['raw_url'];
if (isset($_GET['url']))
{
unset($_GET['url']);
}
// IIS includes get variables in url
$i = strpos($url, '?');
if($i !== false)
{
$url = substr($url, 0, $i);
}
.htaccess
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule .* framework/main.php?raw_url=%1 [QSA]
So here is what is happening:
The .htaccess is now using raw_url instead of url
_ss_environment.php is being called early in the loading process, setting the global $url variable that main.php normally sets. This is set with raw_url rather than url.
To prevent main.php to just override it again when it sees your url query string parameter, it is unset (Silverstripe seems to reset this later as far as my test is concerned).
Lastly is a little block of code that main.php would normally run if $_GET['url'] is set, copied as-is for apparent support in IIS. (If you don't use IIS, you likely won't need it.)
This has a few benefits:
No update to main.php allows upgrading Silverstripe slightly easier in the future
Runs the minimal amount of code needed to "trick" Silverstripe into thinking it is running normally.
The one obvious drawback to any solution for changing away form the url query string parameter is if anything looks at the parameter directly. With how Silverstripe works, it is more likely that code uses the $url global variable or the Director class rather than looking at the query string for the current URL.
I tested this on a 3.1 site by doing the changes I mentioned and:
Creating a controller called TestController
In the init function of the controller, I am running the following:
var_dump($_GET['url']);
var_dump($this->getRequest()->getVars());
Visited /TestController?url=abc123, saw the value of both dumps have "abc123" as the value for the URL parameter.
Navigated to a few other custom pages on the site to make sure they were still working (no issues that I saw)
Unfortunately, I haven't been able to find documentation for the order of inclusion in regards to _config.php and _ss_environment.php. However, after browsing through the code, I have worked out it is this:
main.php runs, first main task is to require core/Constants.php
Constants.php's first task is to search for _ss_environment.php in the base folder and potential parent folders. If it finds it, it will be included.
Going back to main.php (and after the $_GET['url'] check is done in main.php), it will start an ErrorControlChain which it internally does another require for core/Core.php
Inside Core.php, it performs calls for the config manifest
ConfigManifest.php exposes the functions to actually add _config.php files and for them to be required.
I could probably go on however I think this gives a pretty good picture of what is going on. I don't really see a way around not using the _ss_environment.php file. Nothing else gets included early enough that you can hook into without modifying core code.
I had a quick play with this the other day. And looking at what main.php does it might be best to hack away at it rather than ErrorPage_controller.
For startes SS's default .htaccess file does this:
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
# RewriteBase /silverstripe
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1&%{QUERY_STRING} [L]
</IfModule>
Note the ?url changing that to something else and then changing main.php's usage as well may/should help or will cause a heap of extra errors and sadness.
To avoid hacking the core/framework, you could change the .htaccess to target a copy of main.php in mysite (with appropriate include changes).
I am using the codeigniter Tank_auth library and I want to remove the "auth' part from all the urls.
http://mysite.dev/auth/login
to
http://mysite.dev/login
Use the routes configuration, add something similar to this to application/config/routes.php:
$route['login'] = 'auth/login';
Once you got this set up, you can make the webserver to redirect users from the old url like this:
RewriteRule ^auth/login http://%{SERVER_NAME}/login [L,R=302]
This one will redirect old url requests to the newly handled /login, you might want to handle https:// or subdirectories in later part of the rule.
The whole setup seems a little hackish, changing the generated urls seem to be a better idea.
Im trying to get a url rewrite to work and Im not getting any where. basically I want to have three pages one for music and then general products and the other for mvies, each will have a query string of &pagetype=general etc but the url would be http://shopnbuy.vacau.com/browse/computers.html?pageType=general which will redirect to general_products.php and then there would music_products and also movie_products. So they all have the same url except there is a different parameter &pageType=. Below is my htaccess file this is my first attempt at doing this
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
RewriteEngine On
RewriteRule ^browse/([a-zA-Z0-9_-])\.html general_products.php?department=$1&pageType=general
RewriteRule ^browse/([a-zA-Z0-9_-])\.html music_products.php?department=$1&pageType=music
RewriteRule ^browse/([a-zA-Z0-9_-])\.html movie_products.php?department=$1&pageType=movie
url suppose to look like this
http://shopnbuy.vacau.com/browse/computers.html
All Im getting is an error for each url rewrite
I don't know much htacess, but I used it lately and I found out that you can't actually use "/" effectively. I suggest you change it for "_".
As far as I understand, it may be due the "/" works as the one indicating each argument (I mean A / B sorta logic)
Not sure if I explain myself correctly, hoping someone knowledgeable corrects me since I'm eager to learn about it as well.
RewriteRule ^browse_computers / general_products.php?department=$1&pageType=general ([a-zA-Z0-9_-])
RewriteRule ^browse_music / music_products.php?department=$1&pageType=music ([a-zA-Z0-9_-])
RewriteRule ^browse_movies / movie_products.php?department=$1&pageType=movie ([a-zA-Z0-9_-])
The result, however, will be this:
http://shopnbuy.vacau.com/browse_computers
Bit unsure of your initial url but try this
^browse_(.+)/(.+)$ browse/computers.html?pageType=$1
As almost every programmer, I'm writing my own PHP framework for educational purposes. And now I'm looking at the problem with parsing URLs for MVC routing.
Framework will use friendly URLs everywhere. But the question is how to parse them in front controller. For example the link /foo/bar/aaa/bbb may mean "Call the controller's foo action bar and pass parameter aaa with value bbb. But in case someone installs a framework into the subdirectory of the domain root, the directory part should be stripped before determining controller name and action name. And I'm looking for a way to do it safely.
Also I would like to support a fallback case if URL rewriting is not supported on the server.
On different systems different sets of $_SERVER variables are defined. For example, on my local machine from the set of PATH_INFO, REQUEST_URI, REQUEST_URL, ORIG_REQUEST_URI, SCRIPT_NAME, PHP_SELF only REQUEST_URI, SCRIPT_NAME and PHP_SELF are defined. I wonder, if I can rely on them.
Mature frameworks like Symfony or ZF have some compicated algorithms of parsing URLs (at least it seemed to be so). So, I can't just take a part from there for mine.
Two workarounds:
Add config variable with url / instalation directory to your application, and strip it from $_SERVER['REQUEST_URI']
Make apache rewrite it to get variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?myrequest=$1 [QSA,L]
I'm currently doing the same research. But everything I see is so complicated that I'll most probably continue using mod_rewrite anyway. After all you end up with the same thing rather you use SEF with PHP or mod_rewrite with apache. Anyway I'll be monitoring this topic.. it's interesting :)
Hope the php gurus around here have some more info about this :)
Edit:
It really depends on what you want to do. For my needs I hardcoded most of the pages so they looked SEF. But something like the example below should work as well.
RewriteEngine on
RewriteRule ^/posts/([A-Za-z0-9_\-]+)/([A-Za-z0-9_\-]+)\.html$ posts.php?$1=$2 [NC]
With this example above:
http://localhost/posts/view/23
http://localhost/posts/delete/23
is equal to:
http://localhost/posts.php?view=23
http://localhost/posts.php?delete=23
It really depends on what exactly you're doing :)
The example above should be working but I haven't tested them.
I usually use the following for determining an application base URL path, assuming all your requests always goes through the same gateway script:
$base = dirname($_SERVER['PHP_SELF']);
For your second question, if you want to check if mod_rewrite is enabled, you can use:
if (in_array('mod_rewrite', apache_get_modules())) {
// rewrite is enabled
}
However, it doesn't necessarily means that RewriteEngine is enabled, so you probably should use an extra condition:
if (in_array('mod_rewrite', apache_get_modules()) &&
preg_match('/RewriteEngine +On/i', file_get_contents('/path/to/.htaccess'))) {
// rewrite is enabled and active
}
Maybe you could take PHP_SELF and remove the first n chars where n is the length of SCRIPT_NAME.
Edit: Oops... seems like you can just take PHP_SELF: http://php.about.com/od/learnphp/qt/_SERVER_PHP.htm