Replacing the "X" in this url www.websitename.com/info.php?lid=X - mod-rewrite

Help please.
I am looking for the best way to replace the "X" (number) in this url
www.websitename.com/info.php?lid=x
the "x" is a numerical value - i would like to replace the "X" with the "name" field from my database.
Is mod rewrite the way to go? I have multiple urls of the same format (different "X" value of course at the end) that i wish to change to create more friendly urls by replacing the "X" with the corresponding value from the database field "name".
If mod rewrite is the way to go can anyone help out with recommended code to go in the htaccess?
Thanks in advance.

Totally edited: My previous answer was based on a misunderstanding of what you're trying to ask.
What you are asking is to create a friendly URL system. This is covered in many tutorials -- just search for "friendly URLs" and you'll find lots of resources.
Here's a summary of how it works...
To create friendly URLs for your site, you would need something like this in .htaccess (not sure if I got the RewriteRule right because this is completely off the top of my head, so google for a full-blown tutorial to verify):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule /info/(.+) /info.php?name=$1
</IfModule>
This means a request to http://www.example.com/info/foo would be rewritten to http://www.example.com/info.php?name=foo.
Then you need to modify your application (in particular, the info.php file) to handle this new request format in which the name is given in the URL instead of the id.
Note that in this example, all names (e.g., "foo") must be unique. If any two items in your database have the same name, you're going to have problems. With this in mind, you might want to add a new field to your database table, which is a unique column containing a string using only alphanumeric characters and hyphens appropriate for use in a URL (this type of string is called a slug). You will basically use this slug instead of the id for database queries. Let's say you create an item named "The Discombobulator". When this item is created in your application, it should also create a slug along the lines of "the-discombobulator" and ensure it's unique. If you create a second item also called "The Discombobulator", your app might generate a slug for it like "the-discombobulator-2".
So, when someone requests http://www.example.com/info/the-discombobulator-2, mod_rewrite changes that to http://www.example.com/info.php?name=the-discombobulator-2 and hands it to your app. Your app gets the name parameter, which is "the-discombobulator-2" and looks that up in the database's slug field, and gets the matching record.

I think this is what you are looking for:
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

Related

custom routing in magento

I have multi store views in website,
The current URL for a category for the dutch store view would be;
http://www.domain.ext/nl/category.html,
http://www.domain.ext/de/category.html etc
Now I want to add country code with store code. I mean to say, I am getting country code from Users Ip and want to extend it with store code. It will be look like
If the country is set to NL the URL should now be;
http://www.domain.ext/nl_nl/category.html
If the storeview is Dutch (NL) but the country is Belgium (BE), the URL should become;
http://www.domain.ext/nl_bel/category.html
I want this only in category page. I am trying to change in match function of standard.php, but I think its role starts from controller name, please help me how can I achieve it. Any help is highly appreciated.
Are you sure you want to incorporate it into the URL like that?
I think you are going to fight a lot of Magento code to achieve it (product url, catalog url, indexing, getUrl functions and collections, rewrites).
I'm sure you have considered this and the many ways to do what you want but I think you are making work for yourself. Perhaps adding the country code as a URL query string ?c=bel or keeping it all internal to Magento by storing it in a session variable or a general function that you can call at any time eg getCustomCountryCode(). Using a cookie might be a possibility too.
If the URL is purely cosmetic then perhaps an .htaccess rewrite would suffice combined with some rel='canonical' href="http://www.domain.ext/nl_bel/category.html" on the category pages.
If you are determined to proceed then study the class Mage_Catalog_Model_Url

How can I shorten routes in Codeigniter for certain requests?

I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.

Custom profile URL for own site, been though various posts..!

I've been through a few similar posts,
Facebook Like Custom Profile URL PHP
Custom URL / Apache URL Rewriting
But its still not clear, the actual method/process is not available..
Guys , little more guidance would do a lot..
I would like to put forward the questions here:
Users should have a chance to decide what is their url, Just like in case of fb, twitter
for example: www.facebook.com/harry.inaction
I am using the linux, apache, mysql, php environment for this.
Users are identified based on their user id's which get created automatically when they join in
And I fail at the very first step, seriously I don't know get started.
Thanks
It's going to be impossible to put any details as an answer because you've got to build this system of yours and there's more than one way to do it. Design decisions will need to be made based on the way you want things to work and what you already have (they're going to have to work together in some way).
Say you've already got a system for creating users (and it sounds like you do) and you already have a system for viewing profiles. You'll need to extend this system so that you store an extra "my_vanity_url" field in your user table in your database. This field needs to be unique. When a user edits their profile, they have the option of changing this to whatever they want (limiting it to only letters and numbers and dashes for simplicity).
Next, when you display this profile, say it is via /profile.php, your code needs to check a few things.
First it needs to check how it's called, looking at $_SERVER['REQUEST_URI'] you can see either /user/some-vanity-name or /profile.php?u=1234.
If it's the latter, you need to redirect the browser, do a database lookup to see who the user with user_id 1234 is.
Pull the "my_vanity_url" column out of the database for this user and redirect the browser to /user/my_vanity_url_value (replacing my_vanity_url_value with the value of that column).
So now, if you go to http://your.domain.com/profile.php?u=1234, your browser gets redirected and the URL address bar will say http://your.domian.com/user/my_name.
Next, you need to be able to take that unique name and turn it back into the old ugly looking profile page. Two things need to happen here:
You need to extend your profile.php once more to take an optional vanity name as opposed to a user_id
You need to use mod_rewrite to internally route vanity names to /profile.php
For the first thing, you simply look for a different $_GET[] parameter instead of whatever it is for a user_id. Say it's called name: so look at $_GET['name'], see if it exists, if it does lookup the user in the user table whose vanity url name is $_GET['name']. Return the profile of that user.
For the second thing, you just need to put this in the appropriate place in your htaccess file in your document root:
RewriteEngine On
RewriteRule ^/?user/([A-Za-z0-9-]+)/?$ /profile.php?name=$1 [L]
This is just an example for how to implement something like this. It may be completely inapplicable for what you have, but it should give you an idea of what you need to do.

Rewrite a dynamic URL to a new dynamic URL

I am new to the RewriteEngine and have not been able to find an answer to the following issue. I run an ecommerce site with an ever changing catalog of product skus. Our URLs are dynamic. The question is, what if I want to have a dynamic variable redirect to a different dynamic variable.
For instance, I want:
http://www.mydomain.com/product.jhtm?id=12345
to now go to:
www.mydomain.com/product.jhtm?id=78910
How can I do this through the .htaccess?
Thanks in advance.
You will need to use the External Rewriting Program feature of the RewriteMap directive. Effectively, you write an application in whatever language you want (as long as you make it executable), that reads in the original ID on STDIN, and then outputs the new one on STDOUT. You can then reference that application in your RewriteRules.
It may be easier to implement this kind of logic at your application layer, rather than in a .htaccess file, especially since the External Rewriting Program has to be running continously, rather than being called for each new rewrite.
You'll need to use RewriteMap with a map type of prg in order to have a script look up the original ID and return the new one. Don't forget that the query string can only be broken apart in RewriteCond.

Should we check slugs passed in URLS or just use the ID?

For example, if you access this url :Hidden features of mod_rewrite and this one Hidden features of mod_rewrite. It goes right to the same page, and it seems Stackoverflow doesn't check for a valid slug (as wordpress calls it).
I'd use just the ID as the slug may change but you'd still want old links to work.
For example, if someone edited the title of their question you'd want to change the slug appropriately, but you wouldn't want old links to the question to stop working.
Should we check slugs passed in URLS or just use the ID? +)
String is only for user-friendlieness.
I don't see any reason to check the slug if you already have the id. Users shouldn't manually change the URL's, but no harm is done if they do.
Some web applications only have a slug (no id), but then extra care has to be taken to ensure it is unique. Just including and checking the id is much simpler, especially in frameworks like rails.
Since the numerical ID is the only information needed to identify the resource, the slug has only descriptive characteristics. But this description should be appropriate to the resource. So, yes, you should check if the slug is the proper one and correct it if not.

Resources