htaccess rewrite url from different pages

荒凉一梦 提交于 2020-01-22 01:43:20

问题


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] 

回答1:


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.



来源:https://stackoverflow.com/questions/14841937/htaccess-rewrite-url-from-different-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!