嗯,你的规则不考虑PHP文件在这里(检查,我已经在规则的所有正则表达式前要加上斜杠):
RewriteBase /www/
RewriteEngine On
RewriteRule ^/([a-zA-Z0-9_-]+)$ profile.php?u=$1
RewriteRule ^/([a-zA-Z0-9_-]+)/$ profile.php?u=$1
使用这段代码,http://localhost/profile_page.php?u=alice将永远不会匹配。 ^([a-zA-Z0-9_-]+)/$
只能匹配字母,数字和下划线,因此例如问号不能匹配。
试试这个(assumming,与其profile.php你的意思profile_page.php
RewriteBase /www/
RewriteEngine On
RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)$ profile_page.php?u=$1
RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)/$ profile_page.php?u=$1
的另一项改进,你可以只在一个设定两个表达式。
RewriteBase /www/
RewriteEngine On
RewriteRule ^profile_page.php/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
我强烈建议你删除profile_page.php这里为了增加网址的可读性(我看,也许你的意思是说明你的规格)
RewriteBase /www/
RewriteEngine On
RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
RewriteRule ^/profile/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
RewriteRule ^/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
在这种情况下,你将能够匹配这些URL * http://localhost.com/alice * http://localhost:com/profile/alice * http://localhost.com/profile_page.php/alice
为了能够满足自己的个人资料,只是确保您启用以及为部分航线,例如:
RewriteBase /www/
RewriteEngine On
RewriteRule ^/?$ profile_page.php
RewriteRule ^/me?$ profile_page.php
RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
RewriteRule ^/profile/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
RewriteRule ^/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
检查顺序是否在这里。第一个匹配,第一个应用。在那种情况下,我使用了相同的端点文件。检查$ _GET ['u']是否设置为加载指定用户或会话中的用户。
我强烈建议你使用某种前端控制器,以便能够管理所有路由给定一个类(例如app.php),就像Symfony或任何现代PHP框架一样。