问题
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