shadi
shadi

Reputation: 15

htaccess rewrite url from different pages

i make this in htaccess file .. for rewrite url from different page

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]    
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]   

and it's works fine ..

now i need to add more as facebook

you can put facebook.com/username

which is : facebook.com/profile.php?username=

& you can put facebook.com/pagename which is : facebook.com/pages.php?username=

and works fine

from different pages ..

I need to make like this ..

any help ?!

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]    
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]   

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1 [QSA]  
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1 [QSA] 

Upvotes: 1

Views: 347

Answers (1)

Michael Marr
Michael Marr

Reputation: 2099

Your regex patterns match identically, so you're going to need something to differentiate between pages and people (or redirection to profile.php vs. index.php).

You can accomplish this one of two ways. First, you can go all to one page, let's call it redirect.php. On this page, you could check the id value and then redirect. So like:

    RewriteRule ^([a-zA-Z0-9_-]+)/?$ redirect.php?id=$1

In redirect.php, you grab $_GET['id'] and run some query against it to determine if it's a page or person, and then redirect using something like:

    header("Location: profile.php?username=".$_GET['id']);

If you strictly looking for Facebook related stuff, you can hit the graph API up instead of the database to determine if its a page or person ID. (Not sure if it can exist in both - but I think not).

If you don't have a database or data source to determine if its a page or person, then you'll need to add something to your URL strings to determine, like /pages/{pageid}, which you could then rewrite your rules to:

    RewriteRule pages/([a-zA-Z0-9_-]+)/?$ index.php?username=$1  # for pages
    RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1  # for people

If you can't use something in your URL and don't have a way to access it via database, then you're out of luck, as you're hitting two identical patterns.

Upvotes: 1

Related Questions