Remove url segment - php

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.

Related

Routing issue CodeIgniter

i have link
http://www.raffaello-test.com/static/frontend/deutsch/mode-bilder/mode-herren-krawatten/gianni-versace-herren-krawatten.jpg
but i need to hide "/static/frontend" remove this part from URL and get get image from this URL
http://www.raffaello-test.com/deutsch/mode-bilder/mode-herren-krawatten/gianni-versace-herren-krawatten.jpg
i think it will be from .htaccess
One of the option is to create a symlink in the document root.
Second option is to use htaccess rewrite rule. Something like the following should work fine:
RewriteRule ^((deutsch|english)/.+/.+/.+\.(jpe?g|png|gif))$ /static/frontend/$1 [L]
If you have more languages just add them in first parenthesis, like (deutsch|english|russian).
Also note that It's not recommended to use rewriting in high load production for static content, better use symlinks

Recursive redirection in htaccess URL rewriting (mod_rewrite in Apache)

In the root folder of my project there are several other folders I am calling "apps". The path to one of these apps is something like: /root_folder/myapp/...
There are actually a dozen apps like this. So I want my project to be using friendly URLs, and to achieve that I am using the apache module mod_rewrite.
I added the following rule to my .htaccess (that is in root_folder, just so you to know) RewriteRule ([^/]*)/(.*)?$ myDomain.com/$1/index.php?params=$2 [NC,L]
So an URL such as mydomain/myApp/param/value/param2/value would be translated to mydomain/myApp/index.php?params=value/param2/value
I performed some tests and saw it working until I added the $1 to refer to the app folder (have a look: the_path_to_my_root_folder_here/$1/index.php?params=$2)
The path to one of these apps is something like: `/root_folder/myapp/...**
It is generating an URL like: myDomain.com/myApp/index.php?params=index.php
Well I thought it would be a recursion issue. So it seems that Apache will try another redirection after the first is performed, and then it will generate an URL like that
I found this thread in Stack Overflow
The problem with the answer is that it`s assuming I know when to stop.
Do you know how to make this second redirection to stop?
I am trying the following rule now
RewriteRule myDomain.com/([A-Za-z0-9-]+)/(.*)$/ myDomain.com/$1/index.php?params=$2 [NC,L], but it's not working properly. It is only matching the regex, if I do not pass parameters after the app name. so when I try myDomain.com/user/ it works (not receiving parameters), and fails when I try myDomain.com/user/products/1000/, for example. Instead of rewriting/redirecting, it is trying to find a folder products inside user and etc
Try changing your rewrite rule to something that will match the input url but not your output url. Something like this:
RewriteRule ^([^\/]+)\/([a-z]+)\/(.+)$ /$1/index.php?params=$2/$3
Place this rule in DOCUMET_ROOT/.htaccess:
RewriteEngine On
RewriteRule ^([^/]+)/(.+)$ /$1/index.php?params=$2 [L,QSA]
Unable to understand what you mean by the_path_to_my_root_folder_here

Make Codeigniter 2.1.3 allow a Facebook backlink

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

Creating short url for multiple controller?

For my current project in codeignitor I needed to make user profile like this
http://domain.com/userid
Then I tried to add this in router.php
$route['(:any)'] = 'profile/user/$1';
Which is working fine. Now I want to make another URL for language like this
http://domain.com/es
http://domain.com/fr
As for both url uri segments are first, when I type
http://domain.com/es
I see the page of
http://domain.com/userid
I am using .htaccess file for removing index.php in codeignitor. Is there any help how can I achive this task in making shot url for multiple controller. Either with .htaccess or router.php?
Because the routes system works from the top down, if you have multiple rules that can match a url, it picks the first one. So you could do:
$route['(es|fr|en)'] = 'language/$1';
$route['(:any)'] = 'profile/user/$1';
If the first rule matches, it runs, otherwise it tests the profile rule.
You will definitely continue running into issues though with that profile rule, and it would be easier if you did something like:
$route['users/(:any)'] = 'profile/user/$1';
That way it would be more clear what the url is doing, and it will help you for when you are writing rules in the future.

How to solve the case when users surf to index.php

In Zend framework, using the MVC, if A user surf explicitly to http://base/url/index.php instead of just http://base/url, The system thinks the real base url is http://base/url/index.php/ and according to that calculates all the URLs in the system.
So, if I have a controller XXX and action YYY The link will be
http://base/url/index.php/XXX/YYY which is of course wrong.
I am currently solving this by adding a line at index.php:
$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);
I am wondering if there is a built-in way in ZF to solve this.
You can do it with ZF by using Zend_Controller_Router_Route_Static (phew!), example:
Read the manual page linked above, there are some pretty good examples to be found.
$route = new Zend_Controller_Router_Route_Static(
'index.php',
array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);
Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.
Well it really depends on how you want to solve this. As you know the Zend Frameworks build on the front controller pattern, where each request that does not explicitly reference a file in the /public directory is redirected to index.php. So you could basically solve this in a number of ways:
Edit the .htaccess file (or server configuration directive) to rewrite the request to the desired request:
RewriteRule (.*index.php) /error/forbidden?req=$1 // Rewrite to the forbidden action of the error controller.
RewriteRule index.php /index // Rewrite the request to the main controller and action
Add a static route in your bootstrapper as suggested by karim79.
Use mod_rewrite. Something like this should do it:
RewriteRule ^index.php/(.*)$ /$1 [r=301,L]
I don't think you should use a route to do this.
It's kind of a generic problem which shouldn't be solved by this way.
You better should have to do it in your .htaccess, which will offer you a better & easier way to redirect the user to where you want, like to an error page, or to the index.
Here is the documentation page for the mod_rewrite
I've never faced this problem using Zend Framework. just do not link to index.php file. that's it. and when your are giving your application's address to users, just tell them to go to http://base/url/
when the user enters http://base/url/ her request URI is base/url and your .htaccess file routs the request to index.php, but the request IS base/url. you do not need to remove 'index.php' from the request. because it is not there.
when you are trying to generate URLs for links and forms and ..., use the built-in url() view helper to generate your links. like this:
// in some view script
<a href="<?php
echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
?>" >click</a>
do not worry about the link. Zend will generate a URL for you.
The way I look at this is that if I have a website powered by PHP and a user goes to http://site/index.aspx then I would send a 404.
Even though index.php does exist in theory, it's not a valid URL in my application so I would send a 404 in this case too.

Categories