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"];
Related
I'm creating an API via PHP. I need to let users see a particular user. For example if someone goes to mysite.com/users/2, he must get the data about the user with id=2.
This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule ^([-/_a-zA-Z0-9\s]*)$ index.php?url=$1 [QSA,L]
The variable $url in index.php contains the whole url (for example users/2)
I use something like if($url == "users"){ //code } for routes, but what can I do with id that can be various?
Since you have everything in $url, you can just use explode()
Here's an example:
$url = "users/1234";
$url_array = explode("/", $url, 2);
echo $url_array[1];
This doesn't handle any errors, and as you stated in the comments, you'd need to implement those. So, unless this is a very small project it does seem safer, more maintanable, faster, etc. to use a framework like Laravel, or perhaps a micro-framework Lumen or Slim if you only need a few features.
You dont have to include the url in the RewriteRule:
# Redirect all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Then in your index.php:
$urlparts = explode("/", substr(#$_SERVER['PATH_INFO'], 1));
if($urlparts[0] == 'users'){
//find user with id $urlparts[1]
}
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.
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
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]
I have a custom authentication method: in the URL there is a unique ID after slash which is like a login.
When the user passes under the link I need to catch that ID and send it for example to index.php?url_id=****.
The URL is like domain.com/udfgsdfgsd or domain.com/urtywerty.
I'm trying something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url_id=$1 [QSA, L]
But I'm new to the mod_rewrite and so nothing happens...
P.S.
I also have another GET (?panel_status=****) which appears after authorization for navigation and i wondering how to do that the URL after authorization would be like domain.com/some-user-id/some-panel-status or just may be add some prefix to users id like u_***** and redirect when there is u to url_id, in other cases to panel_status
I just described this situation in the answer to this post. Look here:
htaccess rewriting to include file in every folder