how to give each user a unique url - php

favorite
I am building a website and I would like to customize the users' profile so their profile url shares their name. For example, the website domain would be www.example.com and the users' url would be www.example.com/username.
at present when i do a test sign it looks like test/admin/index.php
i want to rewrite it as test/admin/testuser
I am assuming this is a convention because I see this all around the web. Is this done by giving each user their own directory and how painstaking would that be?

Usually this is done by routing. You redirect all requests to the index.php file with .htaccess
Your setup could look like this:
.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?param=$1 [QSA,L]
index.php
<?php
if(empty($_GET['param'])) {
// code for frontpage
} else {
$username = $_GET['param'];
}
?>

In your htaccess you can try this:
RewriteRule ^/([a-zA-Z0-9_-]+)$ index.php?user=$1 [QSA,L]
And in your php:
if(isset($_GET['user'])) {
// code for frontpage
} else {
$username = $_GET['user'];
}
I hope this help you

Related

Display user profile like site.com/user1

I want to create a simple social network with PHP, that every user has a profile that share his image and text.
I have a problem that I want to add functionality in which users will the access to his profile like Facebook , Instagram and etc like below:
my website.com/user1
Not this : mywebsite.com/#user1
Create .htaccess file and add this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /user.php?username=$1 [L] //update user.php?username to your users profile route.
If you have any question about .htaccess file then please check this link.
For use with Nginx, refer to this answer https://stackoverflow.com/a/53415110/6749661
There are a few ways you can do this, like #MRustamzade's answer, you can change the .htaccess file to contain code like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /user.php?username=$1 [L] //update user.php?username to your users profile route.
You can also use a router framework, such as Altorouter which allows you to do this, and with a large number of pages.
With Altorouter you can set a number of routing rules, a simple setup could consist of:
$router = new AltoRouter();
$router->map( 'GET', '/profile/[*:username]', 'views/profile_view.php', 'home' );
$match = $router->match();
if($match) {
header("HTTP/1.1 200 OK");
require $match['target'];
} else {
header("HTTP/1.0 404 Not Found");
echo "Error 404 - File not found";
}
Using this code will allow you to go to site.com/profle/username will go to views/profile_view.php
In order to get the username from the URL in PHP, you can use the $match variable, something like this:
$username = $match["params"]["username"];

Rewrite profile.php?user=username to profile/username

I've created a very basic php templating system that I'm using to display a page with this URL: /basedir/index.php?page=home
The home content is served by the script home.php.
With a rewrite rule in .htaccess, I want my url to look like this: /basedir/home.
The problem that I'm having is that I've got stuck when it comes to rewrite one more GET variable that I'm using to display a user's profile. For now the profile page url looks like this: /basedir/profile?user=username.
Now I want it to look like this: /basedir/profile/username.
So far, my .htaccess looks like this:
RewriteEngine On
RewriteBase /basedir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) ?page=$1 [L,QSA]
I have no idea if this is good or if i have to write a completely new .htaccess file.
Based on your code, and my limited understanding, I would guess something like this:
RewriteRule ^profile/([^?]*) ?page=profile&user=$1 [L,QSA]
.. insert it above your RewriteRule ^([^?]*) ?page=$1 [L,QSA].
As you can see at http://htaccess.mwl.be/ your url http://www.example.com/profile/username is converted into http://www.example.com/basedir/?page=profile/username
Now what you need to do is put index.php in basedir where explode the page and retrieve username.
$page = $_GET['page'];
$parts = explode('/', $page);
switch($parts[0]){
case 'profile':
if(isset($parts[1])){
$username = $parts[1];
return doWhatEverYouWantToDoWithUsername($username);
}
return handleJustProfilePage();
default:
return yourDefaultHandler();
}
This approach is called routing you can make way better with some external routing libraries.

htaccess file for dynamic web pages

my current url : http://example.com/blog-single.php?id=15
this is a dynamic blog page the "id" has to be change accordingly to blog adding
I need to change this url to http://example.com/blog-single/<coresponding-blog-name>
eg: http://example.com/blog-single/my-new-blog
how to Remove .php and id from the current URL?
First of you need to create a .htaccess file and add this code in it
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog-single.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog-single.php?id=$1
After that in your main file for example in index.php file you need to add the code
<?php
$key=$_GET['key'];
if($key=='my-new-blog')
{
include('my_new_blog.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
try this according to your need.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ $1.php?id=$2 [NC,L]
here you can use your filename in manner http://example.com/blog-single/my-new-blog this will work for every page as per your requirement.
For example, if you access to this: http://example.com/blog/1234.html and real path is http://example.com/blog.php?id=1234 you can put this on your htaccess file:
RewriteEngine On
RewriteRule ^blog/([^/]*)\.html$ /blog.php?id=$1 [L]
This tool will help you: http://www.generateit.net/mod-rewrite/index.php

When I host my website I can acess homepage but I always get 404 error when I try to acess my secondary pages [duplicate]

I have my query string to include my pages, in my homepage like you see below and it is working fine, Im including my pages fine.
But something wrong is happening and Im not finding how I can solve this.
I will try to expain my isse with an example: I have a folder "teachers" inside I have two pdf documents and a page "documents.php".
To acess this documents page, Im acessing: "htp://localhost/website/teachers/documents", and it is working fine.
But If I acess "htp://localhost/website/teachers/", Im able to acess my pdf documents and my page as you see in my image below.
But I dont want this, I want that If some user tries to acess "htp://localhost/website/teachers/", I want to include my 404 file (require_once('inc/404.php');)
My query string:
#$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('inc/'.$url[0].'.php')){
require_once('inc/'.$url[0].'.php');
}
elseif(file_exists($url[0].'/'.$url[1].'/'.$url[2].'.php')){
require_once($url[0].'/'.$url[1].'/'.$url[2].'.php');
}
elseif(#file_exists($url[0].'/'.$url[1].'.php')){
require_once($url[0].'/'.$url[1].'.php');
}
else{
require_once('inc/404.php');
}
Do you see what Im doing wrong to not be having the result I want?
My htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
There is an easier solution.
Add this line to the beginning of your .htaccess file:
Options -Indexes
This way, you won't be able to see the folder contents.
EDIT: htaccess rule solution (which is in website folder)
ErrorDocument 404 /website/inc/404.php
RewriteEngine On
RewriteBase /website/
RewriteRule ^teachers/$ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

Friendly URL for my web with htaccess

i have a problem, my site give me this URL: mysite.com/?nav=1&action=inbox and i want to make it friendly, something like this: mysite.com/inbox
How can help me?
Hi, thanks everyone who help me, now i have: mysite.com/1/inbox.html and i want: mysite.com/inbox
Personally I use a generator for .htaccess rewriting.
Create a file called .htaccess and put it in your root folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
This basically says "if the URL is not a file or a directory, go to index.php".
Then your index.php should look like this:
<?php
$q = explode("/", $_SERVER['REQUEST_URI']);
if($q[1]!=""){
switch($q[1]){
case "articles":
include("articles.php");
break;
default:
include("error-404.php");
}
}
else{
include("home.php");
}
?>
Add a new case for each redirect you need. And use $q[2] for the second /.../ and so on.

Categories