user926367
user926367

Reputation:

php, .htaccess - redirect non extension to index + redirect usernames to user page

I've searched a lot in google and stackoverflow but didn't find the complete answer...

What im trying to do is:

1) Redirect all non-extension addresses to their pages. For example: home -> home.php

2) Redirect usernames to a page that searches the username and if exists, then load another page. For example: www.example.com/michael will redirect to go.php and gets "michael" and if user exsists; goes to user.php?id=michael

Its totally like facebook and twitter! Where as if you type "home" it goes to homepage and if you type your username, it goes to your profile!

How can I do such a thing?


EDIT: or maybe a .htaccess that sends anything after the first slash to a page. For example:

www.example.com/this/is/test

will be:

www.example.com/index.php?var1=this&var2=is&var3=test

and then the index.php page will decide to do what...

Upvotes: 0

Views: 107

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

You may try this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/?   [NC]
RewriteRule .*     index.php?var1=%1&var2=%2&var3=%3   [L]

Maps silently

http://www.example.com/this/is/test

To

http://www.example.com/index.php?var1=this&var2=is&var3=test

All parameters are optional with a maximum of 3.

If more are needed, add ([^/]+)?/? for each one at the right end of

RewriteCond %{REQUEST_URI}

line and add keys (varN) accordingly to index.php query in the RewriteRule, using %N to back reference them.

Upvotes: 1

Related Questions