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.
Related
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.
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
For example, say if a user wanted to 'add a place' to my database, how could I create a page almost instantly with that place's name in the domain e.g www.mydomain.com/placename?
I understand it might be a complex solution so if it is too complex to explain can you please push me into the right direction of what I should be researching.
Create functionality to create "pretty urls" in php. Read more about that here: http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html
Create parsing functionality for the urls, so that it recognizes "/placename" as the page "placename"
Create a database structure for pages with the page id, title, content and the url slug, etc.
Create functionality to fetch the right page from the database according to the matching url slug.
Create functionality to render the retrieved informaton
If I understood you right that's one approach to what you want to do.
I'm assuming you're using Apache. If so, create a rule using mod_rewrite that forwards requests for /place/placename to /place.php?name=placename. Then write the place.php script, which will pull the user page from the database and display it in the appropriate fashion.
That's one way to do it - there are others.
First of all try to understand mod rewrite.
You could "mask: a GET url into a much nicer format.
Start here : http://www.elated.com/articles/mod-rewrite-tutorial-for-absolute-beginners/
Then google on and get yourself familiar with all the possibilities.
After that make sure the GET variable is unique in your database, to be absolutely sure use a unique ID.
Example :
domain.com/PLACEID/PLACENAME/
mod_rewrite could then translate this to your php script into :
domain.com/place.php?VAR=PLACEID&VAR2=PLACENAME
Search the data from the user/place through the PLACEID .
Good luck
Here is the real case, in the NewsNow.co.uk, there are many link of uptodate news from thousands of website. And the example for one of the news url:
http://newsnow.co.uk/A/471722742?-19721
all the news url are formated like that, but when we click it, we will be brought to the real url, for ex:
http://www.abcactionnewsx.com/dpp/news/state/bla-bla
anyone know how to achieve this efficiently ?
Store a table of 'internal' paths (the 'newsnow' urls) and the 'destination' urls in a database of some sort; sqlite3 would be a fine choice for smaller applcations.
You could hash the 'internal' paths if lookup time for specific strings was too slow in the database you chose.
When a request comes in, look it up in the database and send back a 302 response with the 'target' URL as the new location for the resource.
This is done using a rewrite engine that is built into common webservers like Apache or Nginx. These engines allow you to write rules that transform a url like the first one into something that would be better understood by your php pages. For example, you could create rules that would turn your first link above into:
http://newsnow.co.uk/index.php?catagory=A&id=471722742&referrer=-19721
This is transparent to the user and they will only ever see the link they first typed in. You can then use the variables being passed in to perform whatever actions you desire. In this case you might want to use the variables to perform some kind of database lookup to retrieve the actual destination that you are interested in. Then it's just a question of performing a php redirect to the link in question.
Check out the following link for a very quick intro to Apache's rewriting capabilities (called mod_rewrite): http://www.besthostratings.com/articles/mod-rewrite-intro.html
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.