working of fancy url - php

in some websites that i saw, urls are fancy and it contains text only. No ids(numbers) or anything.
Example:
In stackoverflow, a sample url to a question is: http://stackoverflow.com/questions/3740229/how-can-i-rewrite-url-to-fancy-url
Here, 3740229 is question id. So using mod rewrite rules we can get question id and pass it to our php page to fetch details of that question.
But in some sites, that will be absent. Example: http://anothersite.com/questions/how-can-i-rewrite-url-to-fancy-url
So how do we fetch details ? that is how to identify question using this how-can-i-rewrite-url-to-fancy-url instead of question id ?

That's often called a URL slug, and it does serve as a unique id for the content it retrieves. The database stores that unique value how-can-i-rewrite-url-to-fancy-url and retrieves its content instead of a retrieving based on a numeric id.
Note that behind the scenes, the database very likely still does have a unique numeric id for each record, but that value isn't exposed to end users, which offers a tiny bit of security through obscurity and prevents crawling scripts from simply incrementing the value to scrape all of a site's content.
When storing new content, process the title accordingly (replace spaces with hyphens, convert to lowercase), and ensure that the value created is unique in your database. If it isn't, you need to append a number or random string onto it to ensure uniqueness. Then use that value instead of the numeric id in your URLs.

Related

Disable HTML URL encoding for GET parameter for query

I'm currently developing a table layout.
The tables are using a paginator and a filter function in PHP.
All values are transmitted as GET parameters.
For example, the paginator will use &limit=20&page=5.
The filter is built upon a table row in thead as input fields.
What I mean is that each column has it's own input field.
Once the submit button is clicked, it will pass the data via GET to itself, so the next pageview will query/filter the data correctly.
For example, if I want to filter the postcode the url will be as following:
&limit=20&page=5&postcode=5
Because I'm allowing searches like %5% to show all postcodes where 5 where the result is not limited to 5 only. It will show all data which has a 5 at any spot of the value.
However, if I want to filter the postcodes showing all results with 58, I will type in %58%. As per URL encoding, unfortunately, the URL won't be &postcode=%58% as expected. It will be &postcode=X%.
The question is whether it is somehow possible to get the correct values into the URL?
The problem lays on browser level. If I would change the URL from &postcode=X% to &postcode=%58% directly and hit enter, Chrome would translate it straight away to X%.
Maybe it's possible somehow with meta tags, http headers, or Javascript, etc.
I'm doing it via GET instead of POST because it was - apparently - simpler to integrate with the paginator.
Sorry for my bad English. Any help would be much appreciated.
Thanks a lot.
You should escape the "%" sign itself (that would be "%25"). PHP should be smart enough to decode that automatically.
So &postcode=%58% should become &postcode=%2558%25, which PHP will decode so that $_GET['postcode'] is '%58%'.
You should urlencode your values before inserting them into the params.
Overall though, If you are using mysql I agree with billrichards.
Since you mention %% searches I assume you are using MySQL or another SQL back end to query for the data. In that case I would suggest leaving the querystring always formatted as postcode=58&page=1, and add some other parameter to indicate if it should be a %wildcard% search or exact match, and if the wildcard parameter is there, add the %% on the back end when performing the query.

SEO friendly ModRewrite + data structure

I'm currently in the process of making my URL schemes much more SEO and user friendly.
I have an index page which passes a variable to a video page, which then loads the corresponding item from my MySQL db. In the database I have fields like so:
ID (Key Value) Name Title
--------------- -------------- ---------------
1 Client Video One
At present my URL's are fetched as:
http://example.com/video.php?=20
Through a bit of ModRewrite experimentation I have the much better
http://example.com/video/20/
However, what I'd really like is to have the following:
http://example.com/video/client/
I'm aware that .htaccess can't access my database so can't automatically switch the ID for the Name value.
My thinking is instead of using the key value (ID in this case) I could simply use the 'Name' field. However I have a few concerns doing this:
if my Name data has spaces in the database entry (as some do - for example client one) how will this affect things?
Also, some 'Name' entries may contain duplicates by having more than one piece of work assigned to them (again, Client One might have Client One - Video One Client One - Video Two etc.)
What's the best way of fetching the data and using ModRewrite to achieve a solution?
first you need to replace space to [_,-,+] then get the NAME of video by ID form database in PHP. try it
i know its not a best answer for you... it just for suggestion.

Converting parameter based URL's to pretty URL's

Currently I have url's in this format:
http://www.domain.com/members/username/
This is fine.
However each user may have several 'songs' associated with their account.
The url's for the individual song's look like this:
http://www.domain.com/members/username/song/?songid=2
With the number at the end obviously referring to the ID in the MySQL database.
Using jQuery/javascript, the ID is collected from the URL and the database is then queried and the relevent song/page is rendered.
I would like to change these URL's to the following format instead:
http://www.domain.com/members/username/song/songname/
But I have absolutely no idea how to go about it. I've been doing quite a bit of reading on the subject but haven't found anything quite relevant to my situation.
To further compound the challenge, song names are not always unique. For instance if we image the song name 'hello' it is quite possible that another song may exist in the database with the same name, albeit with a different song ID.
Given the limit information you are recieving in this question I am quite content with more generalised answers, describing the approach to take.
General info:
Apache/Nginx proxy
Backend: PHP
jQuery/Javascript front end
I don't know how do you store songs in the database but an idea:
use URL rewrite to rewrite members/username/song/songname/ to song.php?user=username&song=songname. There are plenty of tutorials here or perhaps try to use an URL rewrite-generator tool.
In song.php, get these GET values. Do a MySQL query where the songname and the username match. Output the result.
Note: it is OBLIGATORY to make that a user can store only one song with a given name. Also, the storing user's name MUST be stored. Else this is impossible.
Simple Apache rewrites, in the main httpd.conf file, or an htaccess file if you don't have access to the main config file should suffice

PHP Parsing URL

Ok, i'm trying to get data from a database to be pulled according to the url.
I have a database that is holding data for some announcements for individual customer websites. The websites are in individual directories on my website. (i.e., www.domain.com/website1/index.html, www.domain.com/website2/index.html, www.domain.com/website3/index.html, etc..) In the database i have a column that has each customers "filing name" (aka directory name - website1, website2, website3, etc..). I want to try and display only rows in the database where filingName = website1 for the domain "www.domain.com/website1/index.html". Hope this makes sense. I'm basically trying to figure out how to connect the dots between a single page and only pulling a specific customers records. Any thoughts??
Thanks!
Depending on how big your data set is, it might be "cheaper" to preprocess the URLs and store the individual components you need to match on. e.g. create extra fields for host/dir/querystring and store those individually, instead of a monolithic absolute URL. This would be safer than trying to do substring matches, especially if the substring you're matching could be part of multiple different urls ('abc' being part of 'abc.com' and 'abctv.com').
Use a like statement in your query:
SELECT * FROM `sites` WHERE `url` LIKE "%/website1/%";
Your best bet is going to be to store these identifiers on their own in the database (ex 1= website1, 2 = website 2) and name the directories accordingly. Then in your .htaccess file, manipulate the URL to look like your $_GET variable is the directory name, replace it with the matching name in your database. Do this with RewriteRule rule. This is the most absolute way to achieve this while keeping the same URL structure.
edit: whoops, didn't realize how old this thread was.

How do you make non-www. links contain www. in php?

Im storing imdb.com links for each movie thats listed in the DB, and check for duplicates before a new movie is inserted. The problem is, some links are http://imdb.com/whatever while others are http://www.imdb.com/whatever
What would be the best way to force www. into every link thats submitted? I realize I should be storing the url without http:// or http://www. which would alleviate this problem all together.... but its too late to make that decision now.
Why don't you just store IMDB's movie id rather than the entire URL? If you just store the ID then you can build the URL programmatically.
For Instance for this url http://www.imdb.com/title/tt1049413/ you could just store tt1049413. This is a better design in my opinion because if IMDB ever changes their URL format you can just change the part of your app that builds the url rather than changing every row with a bad url.
Use MySQL to fix the existing ones:
UPDATE table SET URL=REPLACE(URL,'http://imdb.com','http://www.imdb.com') WHERE URL LIKE 'http://imdb.com/%';
Then use PHP to fix inbound URLs beforehand:
$url = str_replace('http://imdb.com','http://www.imdb.com',$url);
But the best method is to store imdb.com's movie ID in your database instead:
http://www.imdb.com/title/tt0088846/
Store "tt0088846" instead, or even better, 88846 as your Primary Key, and use a constant:
$imdb_url = "http://www.imdb.com/title/tt{ID}/";
$url = str_replace("{ID}", $movie_id, $imdb_url);
That way it's much faster and easier to detect duplicates. Note that IMDB has different media types (actors, etc.) which use a different prefix (nm for actors, etc.) so be aware when designing your database.
As your storing the link, can't you check if it starts with http://imdb and replace that with http://www.imdb?
To answer your question, forcing non-www. links on submission would be a better option in my opinion, plus I'd update the database using razzed's solution.
$url = str_replace('http://www.', 'htp://', $url);
Still, I would store only the IMDB ID.
You could use regular expression to force the URL but not all host names start with www.

Categories