简体   繁体   English

htaccess从不同页面重写URL

[英]htaccess rewrite url from different pages

i make this in htaccess file .. for rewrite url from different page 我在htaccess文件中制作此文件,以便从其他页面重写网址

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 你可以把facebook.com/username

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

& you can put facebook.com/pagename which is : facebook.com/pages.php?username= &您可以输入facebook.com/pagename,即: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] 

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). 您的正则表达式模式完全相同,因此您将需要一些东西来区分页面和人物(或重定向到profile.php与index.php)。

You can accomplish this one of two ways. 您可以通过以下两种方法之一来完成此操作。 First, you can go all to one page, let's call it redirect.php. 首先,您可以将所有内容转到一个页面,我们将其称为redirect.php。 On this page, you could check the id value and then redirect. 在此页面上,您可以检查id值,然后重定向。 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: 在redirect.php中,获取$_GET['id']并对其执行一些查询,以确定它是页面还是个人,然后使用类似以下内容的重定向:

    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. 如果您严格查找与Facebook相关的内容,则可以点击图表API而不是数据库,以确定其页面或个人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: 如果您没有数据库或数据源来确定它是页面还是个人,则需要在URL字符串中添加一些内容来确定,例如/ pages / {pageid},然后可以重写规则至:

    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. 如果您无法在URL中使用某些内容,并且无法通过数据库访问它,那么您将很不幸,因为您遇到了两种相同的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM