.htaccess 中的目录重写到另一个目录 [英] Directory rewrite in .htaccess to another directory

查看:43
本文介绍了.htaccess 中的目录重写到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有执行此操作的 .htaccess:https://example.com/john 显示目录 https://example.com/user/index.php?用户名=约翰

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆