How get a like twitter url - url

i would like a simple help...
i have a url like this:
example.com/profile.php?id= & name=
my .htaccess file like this.
RewriteRule ^profile/(.)/(.) profile.php?id=$1&name=$2
so i have a end url like this:
example.com/profile/id/name
i can make
example.com/id
but how can i get a url like this:
example.com/name
??
thax

Obviously, your profile.php script is expecting two GET variables, and your desired URL only has one. So you will probably have to change both the script and your database schema.

Your rewrite rule is subtly wrong. Yours will only select a single character in each of the bracketed parts. If you put a * after each dot, it will instead select one or more characters which I think is what you need.
RewriteRule ^profile/(.*)/(.*) profile.php?id=$1&name=$2

If what you're looking for is exactly this:
example.com/name
You will need to change your profile.php to only expect the name variable, and use it to query the database.
I believe previously you had something like:
mysql_query("SELECT * from table where id=$id");
You will need to change it to be
mysql_query("SELECT * from table where name$name");
So you are telling your page to query the user by the name, instead of by the ID.
There's a few drawbacks related to this, as your query won't be as fast as it used to be, as I believe your name column is not the primary key, therefore no indexing.
Twitter uses Rails, so they will be calling it in a slightly different way using something like (onMissingMethod):
get_user_by_username()
Which isn't great either, as it's still querying the database by a string, but has some performance improvements to enable rails to do that.
Your htaccess will then looki like:
RewriteRule ^(.*) profile.php?name=$1
Hope that answers your question

Related

Sql Server Full-Text Search with wild card suffix using Entity Framework 6 Interceptor

http://www.entityframework.info/Home/FullTextSearch
This example works fine for full word searches but does not talk about how to implement wild card suffix.
For example, I can do the following in SQL and get results for "bill" or "billy" using '*' in the end. How do I add that to my Interceptor?
select * from dbo.messagethread a
where contains(Text, '"bil*"')
If you look at that example code in that link above, I was thinking if something like this (below) is possible, but obviously that does not work as it is getting added to the parameter name not the value.
string.Format(#"contains([$1].[$2], #{0} *)", parameter.ParameterName));
There are questions like this one which talk about wildcards in full-text in SQL.
Look for this line in the example link provided in the question.
parameter.Value = value;
Then, to do prefix match, just add this line below that.
value = $"\"{value}*\""; // prefix match
We're basically changing the value of the parameter to have the * in it inside double quotes.
Now if you search for "bil", you get results for "bill"/"billy" and so on.

How to redirect old URL that hasn't any slug to new one which has a slug

Friends, this is a complex problem for me. I have researched on this many times and at last have came to you (with hope that I will get the solution). We had products URLs like:
/product_info.php/products_id/75
As per SEO, I wanted keyword rich URL so, we added a slug in products.php file and modified the URL as:
/product_info.php/products_id/75/product-title
But its also not an ideal URL. I wanted this as:
domainname.com/products/product-title/75
Changes I made in .htaccess file is as follows-
RewriteRule ^products/([A-Za-z0-9-]+)/([0-9]{2})/?$ product_info.php?products_id=$2=$1 [L]
RedirectMatch 301 ^/product_info.php/products_id/([0-9]{2})/([A-Za-z0-9-]+)$ http://www.livevaastu.com/products/$2/$1
Now problem is- our old URLs (which has no slugs) are indexed by Google. And I am not getting any idea how to redirect those old ones to new ones. Also there are many products pages so I couldn't redirect them one-by-one. You guys are genius. Can you help me any how. (without laughing on me). M not a developer.
You can't produce product_info.php?products_id=$2=$1 from your old URLs of /product_info.php/products_id/75 because they don't have the product title ("slug").
For one thing product_info.php?products_id=$2=$1 doesn't make any sense. Is that a typo? What are the key/value pairs in that query string?? This should look something like products_id=$1&product_title=$2 where each derived "value" from the mod_rewrite match gets assigned to a known "key", something you can use in $_GET or $_REQUEST to find the value.
Edit to help with what I think you are trying to achieve here, based on discussion:
If you want your old URLs to lead to the new "pretty" URLs, you will need to use PHP to do this. As mentioned, there simply is not adequate information in the URL to invent the product names. But you could pretty easily have something at the top of each page (i.e. in a header file) which looked to see if the "title" $_GET parameter is present or not (once you clean up the double-equal sign and replace it with proper key/value pairs). This might look something like:
<?php
if( !isset( $_GET['product_title'] ) ) {
// Code here to look up $product_title from the $product_id, presumably in a DB
header("HTTP/1.1 301 Moved Permanently");
header("Location: /products/$product_title/$product_id");
exit();
}

How to check if URLs match, within a huge database of online products?

So, the problem seems simple at the beginning but is not. Using Mongo and Node.js.
Problem: I have a URL. I need to match that URL with all the URLs I have in my database. Remember, there is no rule that the URL I'm on always have "category" infront or things like that. And please don't take "cases" into consideration.
I have no clue of the name of parameters, or anything else.
Let's assume the URL is smth like example.com/category/product_name.html?session_id=2423412fd
In the database I only have example.com/product_name.html
The URL is smth like example.com/index.php?productid=6&category=3&utm_campaign=google&utm_source=click
In the database I only have example.com/index.php?productid=6
The URL is smth like example.com/product_name.html
In the database I only have example.com/category/subcategory/product.html
I think I made my point. What I'm looking is a solution that matches URL in any cases (they are more than these). It can be an external services, class or something complex.
But I need it to work, and to work very fast because is doing this on every page refresh.
Thank you!
I would use this function to separate the strings http://php.net/manual/en/function.parse-url.php
Then take parts of the path name which you want to match from the URL and query your database URL's looking for matches.
To follow on from Anagio's answer, the URL
example.com/index.php?productid=6&category=3&utm_campaign=google&utm_source=click
could be saved as a Mongo object like:
{
url: "example.com/index.php?productid=6&category=3&utm_campaign=google&utm_source=click",
indexes: [
"example.com",
"index.php",
"productid=6",
"category=3",
"utm_campaign=google",
"utm_source=click"
]
}
You could then split up any new URL using the same algorithm, then do a map/reduce on the indexes field for scoring and then take the highest score as the best "fuzzy match"

How to prepare for future URL rewriting to a directory type structure?

So I wish to implement URL rewrite once my site is done but I wish to have it in this format.
site.com/city/example-deal
Currently once a city is chosen it links to a page in the following format:
site.com/city.php?city=atlanta
Then once on that page, a deal is selected from there and it links to the next page:
site.com/deal.php?deal=123
With that in mind, could I rewrite it as such with my current linking structure:
site.com/atlanta/example-deal or do I have to link the page as such:
site.com/city.php?city=atlanta/deal.php?deal=123 in order to get the final URL rewrite structure I'm looking for.
Hopefully I explained this right and thanks for the help!
What you need to do is so that deal.php reads in the city in the query string.
You should also slugify your deal as well so that you can derive the deal id from the deal slug.
Here's an example of a slugify function in php:
http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php
RewriteRule ^([^\.^/]+)/deals/(.*)$ deal.php?city=$1&deal_slug=<deal_slug> [QSA]
Also your deal table in MySQL should be modified to store the slug. With that your deal.php can be modified so that:
// get deal slug from query string
//select from deal table where deal slug = submitted deal slug
// continue with normal code.

How to avoid conflict when not using ID in URLs

I see often (rewritten) URLs without ID in it, like on some wordpress installations. What is the best way of achieve this?
Example: site.com/product/some-product-name/
Maybe to keep an array of page names and IDs in cache, to avoid DB query on every page request?
How to avoid conflicts, and what are other issues on using urls without IDs?
Using an ID presents the same conundrum, really--you're just checking for a different value in your database. The "some-product-name" part of your URL above is also something unique. Some people call them slugs (Wordpress, also permalinks). So instead of querying the database for a row that has the particular ID, you're querying the database for a row that has a particular slug. You don't need to know the ID to retrieve the record.
As long as product names are unique it shouldn't be an issue. It won't take any longer (at least not significant) to look up a product by unique name than numeric ID as long as the column is indexed.
Wordpress has a field in the wp_posts table for the slug. When you create the post, it creates a slug from the post title (if that's how you have it configured), replacing spaces with dashes (or I think you can set it to underscores). It also takes out the apostrophes, commas, or whatnot. I believe it also limits the overall length of the slug, too.
So, in short, it isn't dynamically decoding the URL into the post's title--there's a field in the table that matches the URL version of the post name directly.
As you may or may not know, the URLs are being re-written with Apache's mod_rewrite module. As mentioned here, Wordpress is, in the background, assigning a slug after sanitizing the title or post name.
But, to answer your question, what you're describing is Wordpress' "Pretty Permalinks" feature and you can learn more about it in the Wordpress codex. Newer versions of Wordpress do the re-writing internally (no .htaccess editin, wp_rewrite instead). Which is why you'll see the same ruleset for any permalink structure.
Though, if you do some digging you can find the old rewrite rules. For example:
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$ /index.php?year=$1&monthnum=$2&day=$3 [QSA,L]
Will take a URL like /2008/01/01/ and direct it to /index.php?year=2008&monthnum=01&day=01 (and load a date category).
But, as mentioned, a page like product-name exists only because Wordpress already sanitized the post title and stored it as a field in the database.

Resources