Username of social network - php

I need to set username for my website..
like i have a profile id of user say localhost/ajax/profile.php?id=27
I need to change it like localhost/ajax/xyz
How to achieve it? Please give me valuable link or show me the right way to go or help me out.
i am totally newbie in this town so please help me from 0.
i mean can i have to make php file before applying .htcaccess ? if yes then what to write on that php file?
suppose a person choose a username "abc" so how can the script identify that localhost/ajax/profile.php?id=111 is "abc" and we need to rewrite localhost/ajax/abc
In summary : how to make username like Facebook.

Use .htaccess to hide your file name.php extension
RewriteOptions inherit
RewriteEngine On # enables url rewriting
RewriteCond %{REQUEST_FILENAME} !-d # if requested uri is not directory (!-d)
RewriteCond %{REQUEST_FILENAME}\.php -f # and if there is a file named URI+'.php' (-f)
RewriteRule ^(.*)$ $1.php # then if there is any thing in uri then rewrite it as uri+'.php'

You need to use .htaccess to re-structure your links. You need to create.htaccess if its already not there inside your main folder. You can try this out:
RewriteRule localhost/ajax/(.*) localhost/ajax/profile.php?id=$1 [PT,L,B]
Remember that on RewriteRule, you can't use any php scripts. So, You can't rewrite localhost/ajax/profile.php?id=27 to localhost/ajax/xyz readily. You need to pass localhost/ajax/profile.php?id=xyz and above rule will work fine.
Learn more about .htaccess RewriteRule from here.

u can do it with .htaccess url rewrite method.
it will convert ur url localhost/ajax/profile.php?id=111 to localhost/ajax/111
after then u can also modify to localhost/ajax/myusername
try given link and enjoy 100% satisfied answer
http://www.9lessons.info/2009/11/pretty-urls-with-htaccess-file.html
here u will find some extra tools for ur social network

Related

.htaccess - Set GET Variable everytime at the end

Hi,
momently I turn my Website to an multilanguage Website.
Problem:
I would like to Read the locale of the user from an GET variable. So the htaccess must write this locale at the end.
If this question already exists please send me the link, or is there an simpler way for the local tell it me.
You could do that using ModRewerite
RewriteCond %{QUERY_STRING} !locale=
RewriteRule ^(.+)$ $1?locale=DE [QSA,NC,L]

Custom link for each user in PHP

The standard GET query as a link for each user is easy but ugly for the users and search engines.
// This looks UGLY
website.com/index.php?userid=43232
//This is what I want
website.com/coolUser
How to make a custom link for every user without creating folders all the time?
It's called "routing". You may begin with this article: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
What you mentioned called "clean URL", and what you need something known as "mod-rewrite" capability in Apache configuration.
Sergei and Zulkhaery direct you to it with their comments. Also you can take a look at the link below:
http://wettone.com/code/clean-urls
But don't forget you may see some problems with you css, js, and image files. But they have their own solutions too. You can find them if you get these problems.
Simple way to make URL like that, using .htaccess (create .htaccess file and upload to your public_html / wwwdir)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?userid=$1 [L]
Now, you can access http://yoursite.com/43232 OR http://yoursite.com/index.php?userid=43232

.htaccess mod_rewrite redirect issue

I have currently hit a bit of an issue with redirecting users with .htaccess and was wondering if anyone could help me.
1. Background:
I currently have a rather long domain name for the sake of this question lets refer to it as mylongdomainname.com now on this domain I have a subdomain that I use to host files, pictures etc to show friends or share with people this is files.mylongdomainname.com
now obviously the URL can get quite long as I have different directories and files. so to help reduce a bit of space I purchased another short domain, lets refer to this as small.me now what I want to do is use .htaccess and a simple PHP file to redirect small.me to files.mylongdomainname.com and pass on a file reference.
Example:
small.me/pictures/example.jpg should redirect to files.mylongdomainname.com/pictures/example.jpg
2. The problem
Basically I am unsure on the exact rewrite rule I would need to acomplish this. obviously I need a rule that will allow anything after small.me/ to be sent with the GET method to the index file which would then redirect the user accordingly. So that means that the rewrite rule will have to allow all letters, numbers and valid file name symbols to be used. I'm sure it's simple but after looking at a few tutorials and mod_rewrite help sites I still have no idea how to accomplish this.
3. The Code
.htaccess
RewriteEngine on
RewriteRule ^$ index.php?file_location=$1 [L]
obviously wrong
index.php
<?php
//Get the requested files location.
$file_location = $_GET['file_location'];
//Redirect the user to the file.
header('refresh:2; url=http://files.mylongdomainname.com/' . $file_location);
?>
4. Notes
I am aware I could just use a URL shortener, but because I am awkward I would rather it just went through my own domain, so please don't comment or answer telling me to use a shortener or to use a service like dropbox.
So can anybody help me by providing the right rule? Any help is much appreciated.
In .htaccess you can simply use:
RewriteRule ^(.*)$ http://small.me/?$1 [L]
No need for the PHP file if that's all you're trying to do.
Assuming you want this:
Picture to be shared: http://files.mylongdomainname.com/pictures/me/troll.jpg
Desired URL: http://small.me/pictures/me/troll.jpg
Remove the PHP file, just Place this in small.me's htaccess:
RewriteRule ^\/?(.*)$ http://files.mylongdomainname.com/$1 [NC,L]
The following in your .htaccess file should be all you need (no PHP file needed):
RewriteRule ^(.*) http://files.mylongdomainname.com/$1 [RL]
For more information and examples see the mod_rewrite documentation

Pretty URLs from DB

I am working on creating page links from DB like the following example.
Current page:
www.example.com/page.php?pid=7
In the DB it is saved as title "contact-us" under category "Company Info"
I want it to be like:
www.example.com/company-info/contact-us.html
I have tried different solution and answers but did not got any luck. I am not sure, where will be the PHP part and which rules to write for .htaccess files.
In apache (or .hataccess) do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /proxy.php?_url=$1 [QSA,L]
So in a nutshell, if the resource being requested doens't exist, redirect it to a proxy.php file. From there $_REQUEST['_url'] will be the url the user was requesting.
Then create proxy.php in your home directory and add whatever logic you'd like to load the correct content.
If you use this from .htaccess, then you may need to add RewriteBase / to your config.
If you want to find this page by url, you will probably do this through php and .htaccess. Make a .htaccess that calls page.php for each and every request. You don't need the pid=7, because, well, how should the .htaccess know it is 7, right? :)
In page.php, you take the original url and split it on the slashes, so you get the category (company-info) and the page itself (contact-us.html). Then, you can look these up in the database. This is in a nutshell how many software works, including Wikipedia (MediaWiki) and CodeIgnitor.
Mind that 'company-info' isn't the same as 'Company Info'. You'll have to specify the url-version in the database to be able to use it for look-up.

Custom URL with PHP

I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?
I want it to be Facebook style...Where "username" is the username chosen by idu = x.
There are multiple ways to solve this problem, but here's one that always suits my needs.
Guide all your URL requests through the index.php first and resolve the request in your PHP code second.
1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.
2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.
Good luck and beware of URL injections using this method.
You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.
You can try to use this mod_rewrite pattern (add it to the .htaccess):
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1
you have to write a clean URL in your .htaccess file like :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1
Put the following in your .htaccess
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]
The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.

Categories