从不同的页面的htaccess重写URL重写、不同、页面、htaccess

2023-09-02 09:39:18 作者:暮色年華

我做这htaccess文件..从不同的页面重写URL

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([A-ZA-Z0-9 _-] +)$ profile.php?用户名= $ 1 [QSA]
重写规则^([A-ZA-Z0-9 _-] +)/ $ profile.php?用户名= $ 1 [QSA]
 

和它的正常工作。

现在我需要添加更多的Facebook

IIS7.5导入.htaccess伪静态规则 URL重写的方法教程

您可以把facebook.com/username

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

&放大器;你可以把facebook.com/pagename 这就是:facebook.com/pages.php?username=

和正常工作

这是不同的页面。

我需要作出这样的..

任何帮助吗?!

 重写规则^([A-ZA-Z0-9 _-] +)$ profile.php?用户名= $ 1 [QSA]
重写规则^([A-ZA-Z0-9 _-] +)/ $ profile.php?用户名= $ 1 [QSA]

重写规则^([A-ZA-Z0-9 _-] +)$的index.php?用户名= $ 1 [QSA]
重写规则^([A-ZA-Z0-9 _-] +)/ $的index.php?用户名= $ 1 [QSA]
 

解决方案

您正则表达式匹配的模式相同,所以你会需要一些页面和人民之间的区分(或重定向到profile.php对的index.php) 。

您可以完成这两种方式之一。首先,你可以去所有的一页,让我们称之为redirect.php。在这个页面,你可以检查id的值,然后重定向。所以这样的:

 重写规则^([A-ZA-Z0-9 _-] +)/ $ redirect.php?ID = $ 1
 

在redirect.php,你抢 $ _ GET ['身份证'] ,并针对它运行一些查询,以确定它是否是一个页面或个人,然后重定向使用的东西这样的:

 头(?地点:profile.php用户名=$ _ GET ['身份证']);
 

如果你仔细观察对于Facebook相关的东西,你可以访问数据库的图形API,而不是向上,以确定其页面或个人ID。 (不知道是否可以同时存在于 - 但我不这么认为)。

如果你没有一个数据库或数据源,以确定其页面或人,那么你需要的东西添加到您的URL字符串来确定,如/页/ {}的pageid,然后你可以重写规则:

 重写规则页/([A-ZA-Z0-9 _-] +)/ $的index.php?用户名= $ 1号的页面
    重写规则^([A-ZA-Z0-9 _-] +)/ $ profile.php?用户名= $ 1号的人
 

如果您不能使用的东西在你的网址,并没有办法通过数据库来访问它,那么你的运气了,因为你打两个相同的图案。

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] 

解决方案

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.