目录以.htaccess重写到另一个目录 [英] Directory rewrite in .htaccess to another directory
本文介绍了目录以.htaccess重写到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有执行此操作的.htaccess:https://example.com/john
显示目录https://example.com/user/index.php?username=john
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]
如何使https://example.com/john/blah
显示https://example.com/user/blah
的内容?如果应用了我以前的重写规则,则仅显示/user
文件夹的内容.
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.
例如:
example.com/john/haha ---> example.com/user/haha
example.com/privacy/blah ---> // Will show 404 page because /privacy is an actual page.
推荐答案
您可以在网站根.htaccess中尝试以下规则:
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]
这篇关于目录以.htaccess重写到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文