2013-02-04 72 views
0

我想使用户名作为URL的一部分,URL应该是这样的网址与PHP的.htaccess重写

domanin.com/username/profile.html

domanin.com/username/events.html

domanin.com/username/messages.html

你可以指导我这个怎么做?

如果我与该网站注册为阿里然后阿里应该是我的网址的一部分,就像

domain.com/ali/profile.html

domain.com/ali/events.html

+0

什么你想你的重写的网址看起来像什么,将是实际的网址?用例 –

+0

@VarinderSingh详细说明用户名将被替换为用户在注册时选择它的用户名。 – MIrfan

回答

1

这是我如何在我的网站,用户可以注册这样做,它在PHP中,但可以修改,以获得你想要的。

RewriteEngine On 

# Full path 
RewriteBase/

# Rewrite all php files  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

# Rewrite the user profile pages 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^user/([a-zA-Z0-9_-]+)$ profile.php?user=$1 
RewriteRule ^user/([a-zA-Z0-9_-]+)/$ profile.php?user=$1 

第一个RewriteBase是您网站的根目录。 “#重写所有php文件”下的第二个重写将从文件中删除所有.php文件扩展名。

第三个重写集是你感兴趣的。Profile.php是用户配置文件,而?user = $ 1是注册用户名。当调用URL www.domain.com/user/userName/时,它实际上会从页面profile.php?user = userName获取数据。

你将不得不修改这一点来工作你想要如何,看看我是如何在PHP中,但有关于如何做到这一点的谷歌教程。

+0

这个网站有一个体面的指南,用.htaccess进行网址重写。 http://www.branded3.com/blogs/htaccess-mod_rewrite-ultimate-guide/如果它没有你想要的,只需google .htaccess mod_rewrite,你将得到吨的结果。 –