Rewriting a CI url - php

I have never before rewritten a URL except for removing the index.php part of my CodeIgniter installs, and that I do by using a copy-pasted snippet in my .htaccess file. I haven't had the time to actually learn about what the snippet does; I'm basically very new to rewriting URLs.
I have a mobile version of my web application. I got as far as redirecting mobile users to a subdomain: m.myhost.tld. However, since I'm using (one) CodeIgniter (install), I have to send these mobile users to a mobile-specific controller, in my case /mobile/. So, the controller always shows up in my address bar.
I just don't think this is very clean, and I'm looking for a way to rewrite the URL; but truth be told, I'm not even sure if this is possible.. hence my question.
I want to get rid of the /mobile controller part. Is this possible?
Some examples:
My current mobile 'root' folder is
http://m.myhost.tld/mobile
I would like to turn this one into
http://m.myhost.tld/
At the moment, when I go to http://m.myhost.tld/, it redirects to the default controller for my CodeIgniter application, which is part of the 'desktop' version of the web app.
Another example:
Turning
http://m.myhost.tld/mobile#mobile/about
into
http://m.myhost.tld/#mobile/about
I'm not sure if I'm making any sense here. I am in my head, but like I said, I don't know what exactly is possible. If the user is on the m subdomain, I want to hide the /mobile part of my URLs. However, only when we're on the m subdomain, so the 'desktop' version (which sits at www ) does not get touched at all.
Like I said a couple of times now, I'm not sure what is possible and what I'm looking for might just be too complex or whatnot. I figured I would ask though, since learning by asking is what I do best. Please don't be too hard on me if this turns out to be a dumb question, sirs professionals ;)
EDIT:
I thought I'd edit because I don't want to come off wrong. I'm not necessarily looking for exact answers to my question. I also welcome documentation/tutorials/articles that might guide me to a solution. If I can manage to come up with a solution of my own, I will of course learn a lot more.
Thanks a lot.

This could be too simple of a solution but why not in your routing config do something simple like
in your config do something like
if ($_SERVER['SERVER_NAME'] == 'm.myhost.tld')
$route['default_controller'] = "mobile";
This would make the default controller mobil so you wouldn't have to have /mobile...
As i said maybe too simple

EDIT: Doesn't work, but maybe someone can turn it into something that does
Try this:
RewriteCond $1!^mobile/
RewriteCond %{HTTP_HOST} ^m\.myhost\.tld
RewriteRule (.*) /mobile/$1 [L]

Related

Is it Possible to Drop Uri Segments with Codeigniter Routing

I have the url: http://devsite.com/page/profile/11
I would like to retain the current functionality of /controller_name/method/profile_id
BUT, I would like the url to drop the controller name, so it would just be: method/profile_id
I'm thinking this is not what codeigniter uri routing is meant for. I understand to a degree and use uri routing all the time, but it seems that this is more of something I'd need to do with apache mod_rewrite or something? Any ideas are appreciated.
Thanks.
Small bug in your route. There shoud be $1 at the end, not $2:
$route['profile/(:any)'] = 'page/profile/$1';
I figured it out, I wasn't thinking about it correctly. I have not BUILT the application yet, rather I'm building it as we speak. So if I HAD built the entire application and I was just now asking this, it would indeed be an apache rewrite situation, but since I am building the app now, some "pre-thought" will save me the trouble.
Instead of linking to 'page/profile/id', I can just link to 'profile/id' and then in my routes.php put this:
$route['profile/(:any)'] = 'page/profile/$2';

Htaccess rewrite for user profile and possible conflictions

I need to ask something about htaccess redirection. I know there are lots of questions about htaccess, rewrite and pretty profile urls, but I've never found real answer of my question and I hope I can find with your help.
That pretty url rules as you know would work like changing "mydomain.com/profile.php?username=myuser" to "mydomain.com/myuser".
But let's say I have a rewrite rule for my login url : www.mydomain.com/login
That means if user try to have exact same username as "login" how could you handle that possible confliction on rewrite?
actually possible solution might be minimum character limitation like minimum 6 chars, but it's not looking elegant since you loose your option to use more than 6 chars like "/resetpassword".
Probably a "banned words" kind of array control would be a solution when user picks a username but then you need to foresee all kind of possibilities which shouldn't be used.
Many of giant websites use this rewrite methods. Particularly Facebook uses "/username" kind of rule for pages and users in the same time.
Anyway, if someone has what is the magic behind that kind of url redirection/rewrite rules please help me out on this :)
Thanks
P.S. : I know there is another solution like "/user/username" but nowadays pointing directly to the base url and shortening full url is getting more and more popular, and I just need to understand possibilities on that.
Why not just have a login sub directory in the root of your site that contains the relevant files for logging a user in? That way, the rewrite rules in your htaccess file only have to deal with the whole user redirect stuff.
What you're looking for is something called "routes". They're typically implemented by MVC frameworks like Zend Framework, CakePHP of Symfony.
What they essentially do is forwarding every request to some index.php which in turn figures out from $_SERVER['REQUEST_URI'] which PHP files should handle the request.
I wouldn't recommend putting rewrite rules into your .htaccess file by PHP. Instead, try getting into PHP frameworks. They do the hard lifting for you.
Personally, I use Zend Framework. But I wouldn't recommend the new version 2 to beginners. Try ZF1. It's actually pretty easy to get into.

PHP OOP routing

i want to make a very simple website but with OOP PHP. I got enough experience in programming (c#, c++, php, js and more) so i know how to make classes etc, but the thing i dont understand with php is the correct way to call things.
there are hundreds of tutorials oop php on the internet but nothing with this (or maybe its a weird question :P).
let me explain.
for example i want a news website and i got a class News with the function create.
if i follow the url mywebsite.com/news/create or mywebsite.com/news?action=create i want to execute the php class News, action create.
but how am i suppose to do this. do i need to make in index.php
if(action == news) news->create();
and for every action another... i dont think so :P. so how can i make this correctly? or is it better to take a simple mvc framework?
Thnx,
Stefan.
I would use the CodeIgniter framework for this, it is EXTREMELY easy to install, plus it uses the the MVC design pattern.
Then to make your url like this: "mywebsite.com/news/create" you can change a simple thing in the htaccess file like such:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Source:
http://codeigniter.com/user_guide/general/urls.html
If on the other hand you don't want to use a framework, you can just use Apache's mod_rewrite to remove the script filename, then using php's explode function to get the function and parameters from the $_SERVER["REQUEST_URI"] variable.
There is a good example here:
http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
i know many frameworks and i worked with codeigniter, and yii. I prefer yii but the mean question for me is, is it possible to work without such frameworks and route things or is it better to take a framework.

Rewriting url to hide index.php and to make query nice

I had developed project with custom MVC architecture. And I am new to Apache world, so I would appreciate help with this matter. On a Web I had found lots of tutorials, but no one like mine interests.
I have URL like this: http://knjiskicrv.comoj.com/index.php?page=book&id=1
I would like to be display like this: http://knjiskicrv.comoj.com/book/id/1
Or this: http://knjiskicrv.comoj.com/index.php?page=faq
Into this: http://knjiskicrv.comoj.com/faq
If there is no page in query (http://knjiskicrv.comoj.com/index.php), I would like to show: http://knjiskicrv.comoj.com/
Also with no page in query (http://knjiskicrv.comoj.com/index.php?category=2), it should be like this http://knjiskicrv.comoj.com/category/2
Hope someone will help. Thanks.
Actually, your problem is a two step proble. You first need to understand what is "Routing" in MVC. If you have your own implementation of an MVC like framework and you don't support routing, then it probably means you didn't even know how it worked before. (Sad but true)
In an MVC framework you setup routes using a ROUTER and the router analyses the urls for you saying HEY, i found this url that matches your request, go ahead and do work with it.
So then, your controller receives a request to route into itself and PARSES the url as he sees fit. Such as using explode('/', $_SERVER['REQUEST_URI']) and then reading the different parts of the url to map to expected variables.
All of this is very theoretical because there are ZILLIONS of way to implement it in a custom way. The only thing you will have to use is a little mod_rewrite magic to pass all requests to your index.php that will route everything. Look at the url below to learn about mod_rewrite, it's a VERY COMPLEX subject:
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
What i usually go for but i don't have access to it from home is something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^assets/
RewriteRule .* index.php
This will redirect all traffic to index.php and you can then use $_SERVER['REQUEST_URI'] to analyze the request. Everything in assets/ folder will not be touched and work correctly.
Note, i built that part off my hat, it might not work...

How should I manage a PHP database-driven website with lots of pages/content?

Let it be known that I only have experience making websites with 5 or 6 pages. I'd like to make a PHP Gaming journalism site like http://www.escapistmagazine.com/
The first problem I can think of is that I would have to manually make a page for each game article; that path was obviously not going to work so I decided to store all the articles in a database.
The problem with storing the content in a database was figuring out how to retrieve them. I attached a GET variable to the url so I could retrieve any article from an index.php file; however, I could not hide the GET variable from the url so I ditched that method. I have no free cash to buy a CMS and I have tried free ones like Drupal to great frustration.
Do I have to generate a separate php file for each article? What would a professional/veteran do in my situation?
First, I'd like to state that if you are having trouble finding the patience to set up Drupal, Wordpress or similar free CMS', you would be hard pressed to find the patience to creating one from scratch.
Having said that, to address the specific request you're looking for, I believe you want to use mod_rewrite to funnel your requests through a single file, which would, in turn, hand the request off to the appropriate file. Drupal, for instance, uses the following rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Which routes your URL into a single variable, 'q'.
I manage a CMS that publishes PHP pages statically like you're considering. Coming from MVC designs, it is horrific, and I highly suggest you not take that route.
I'd check into one of the frameworks like Code Igniter, Cake, or if you feel like you want to torture yourself, Zend. All joking aside, you'll be able to create routes that use the URL request to find the content that you're looking for. I won't go into the whole concept of MVC and routing here, as it is well documented throughout the web, but essentially it keeps template management much easier. As a matter of fact, pretty much everything is easier, and your codebase stays much cleaner.
Right now, I've got a codebase for my CMS of almost 400mb. This is because there's an abundance of static pages that are indexed. This would be cut significantly to ~50mb (if that) if I converted it to an MVC framework. Keep in mind, this is without user generated content like PDF, MP3, etc.
If that seems scary, I highly suggest using Joomla!, Drupal, Wordpress, or any of the other CMS systems out there. Trust me, you'll save a ton of time.
The best advice I could give you: use Wordpress, don't rebuild it yourself. It's really perfect for this job.
Bottom line: 15% of the best websites run using Wordpress.
When you start considering security, maintainability, time, and other factors - for what you are describing I would just use WordPress. Free, easy to setup and proven. It's not just making the frontend site for your viewers, you also need all the admin tools to manage it all as well.
If you do build your own, you will want to use a database to store your data. You won't create a php file for each article, you will most likely have one or few php files that focus only on loading an article page from the database using rewrite rules for nice urls. Good luck with the path you choose.

Categories