用户名,文件夹为mod_rewrite

时间:2016-08-05 13:47:56

标签: php apache .htaccess mod-rewrite

我有这个问题,我有这个url example.com/directory/fotos/index.php?u=username,我需要做的是将其更改为 example.com/username/fotos 我已经在htacces中有一个规则,如果在index.php中找到用户名,则会将目录更改为用户名

RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ directory/index.php?u=$0&%{QUERY_STRING} [L]

哪个输出example.com/someusername

如何在不与当前规则冲突的情况下为上述案例制定新规则?

1 个答案:

答案 0 :(得分:2)

这应该做:

RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/([^/]+)/fotos/?
RewriteRule ^([^/]+)/?$ /directory/index.php?u=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/directory/fotos/?
RewriteRule ^([^/]+)/fotos/?$ /directory/fotos/index.php?u=$1 [L,QSA]