2

I want to pass username in url just like what other social networking sites like facebook do. The url should be like : www.mysite.com/username

Also I want to be able to access directories if the value at place of username is a directory name.

Ex. www.mysite.com/downloads will take to the downloads directory.

For this what I can do is to first check where the value passed is a valid directory name. If yes, access directory else check for whether user exists with the username passed.

for this what I have done is, created a .htaccess file in root directory containing

RewriteEngine On
RewriteRule ^ index.php

and checking for the url

// requested url
$request = $_SERVER['REQUEST_URI'];
// trim last / if available
$url = rtrim($request,'/');
// explode url and save in $ar array
$ar = explode('/',$url);

if(sizeof($ar) > 2)
{
  header('location : error/?error=1');
} else {
  $user = $ar[1];
}

The if statement above is to check for passed parameter is one or more. Ex. www.mysite.com/username/post will result in an invalid url

But by this code, I couldn't access my directory www.mysite.com/downloads what is the easy way to do this.

Edit 2 :

I want the url www.mysite.com/username to pass username as variable if is not a directory or file to index.php in myProfile directory where it is access as $user = $_GET['u'];

updated .htaccess

# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
Options All -Indexes

# Timezone
SetEnv TZ Asia/Kolkata

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myProfile/index.php?u=$1 [L,QSA]
5
  • Possible duplicate of How to rewrite URL for user profile in .htaccess Commented Apr 15, 2016 at 12:08
  • Thanks @Henders for sharing the link but following the answer there, it is giving 500 Internal Server Error. Commented Apr 15, 2016 at 12:27
  • @anubhava I have updated .htaccess and a little more description. Commented Apr 16, 2016 at 4:25
  • www.mysite.com/username is giving 404 error and is showing 404 error page Commented Apr 16, 2016 at 19:35
  • .htaccess is in root directory ie., at www.mysite.com which contain a subdirectory myProfile and an index.html page. Yes www.mysite..com/myProfile/index.PHP?u=username is working from browser Commented Apr 17, 2016 at 6:01

1 Answer 1

2

Try this code in /.htaccess:

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
DirectoryIndex index.php

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myProfile/index.php?u=$1 [L,QSA]

If you still get 404 then verify whether your .htaccess is enabled or not, by putting same garbage (random) text on top of your /.htaccess and see if it generates 500 (internal server) error or not when you visit www.mysite.com/index.php?u=username URL in browser.

Sign up to request clarification or add additional context in comments.

1 Comment

This is working as expected but it is working on www.mysite.com/myProfile/username and not on www.mysite.com/username

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.