.htaccess rewriterule用于像facebook这样的用户名

时间:2014-09-25 12:17:46

标签: apache .htaccess mod-rewrite

我正在为我当前的应用程序使用以下代码

RewriteBase /

#for profile display
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([^/]+)/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2&source=$3 [QSA]
RewriteRule ^user/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2 [QSA]
RewriteRule ^user/([^/]+)/?$ /profile.php?username=$1 [QSA]

#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

这样做会将网址www.domain.com/user/johndoe/photos/photoid-123视为www.domain.com/profile.php?username=johndoe&option=photos&source=photoid-123

现在,我要做的是从网址中删除/ user / part并将其保留为www.domain.com/johndoe/photos/photoid-123

我尝试修改重写规则,例如RewriteRule ^([^/]+)/....RewriteRule ^/([^/]+)/....RewriteRule ([^/]+)/....,但它给了我500错误

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

这样做:

DirectoryIndex index.html index.php     RewriteEngine On     RewriteBase /

#ignore files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2&source=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [QSA,L]

#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !\.php$ %{REQUEST_FILENAME}.php [QSA,L]