Directory rewrite in .htaccess to another directory

自闭症网瘾萝莉.ら 提交于 2020-01-05 03:46:37

问题


I have .htaccess that does this: https://example.com/john shows the contents of the directory https://example.com/user/index.php?username=john

RewriteRule ^([a-zA-Z0-9_-]+)$ /user/index.php?username=$1 [QSA]

How can I make it so https://example.com/john/blah will show the contents of https://example.com/user/blah? Only show contents of /user folder if my previous rewrite rule was applied.

For example:

example.com/john/haha ---> example.com/user/haha
example.com/privacy/blah ---> // Will show 404 page because /privacy is an actual page.

回答1:


You may try these rules in your site root .htaccess:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/user/$1.php -f
RewriteRule ^[\w-]+/([\w-]+)$ user/$1.php [L]

RewriteCond %{DOCUMENT_ROOT}/user/$1/ -d
RewriteRule ^[\w-]+/([\w-]+)/?$ user/$1/ [L]

# existing rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ user/index.php?username=$1 [QSA,L]


来源:https://stackoverflow.com/questions/59252408/directory-rewrite-in-htaccess-to-another-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!