【问题标题】:Directory rewrite in .htaccess to another directory.htaccess 中的目录重写到另一个目录
【发布时间】:2020-04-02 18:21:29
【问题描述】:

我有这样做的 .htaccess:https://example.com/john 显示目录 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 文件夹的内容。

例如:

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

【问题讨论】:

  • @anubhava 抱歉,编辑了帖子。应该是user/profile.php
  • 所以/john/profile 应该重写为/user/profile.php?username=john ?
  • @anubhava 我更改了帖子以使其更清晰。我想保留到目前为止的内容,但我也想:/john/* 显示/user/* 的内容,如果应用了第一个重写规则。

标签: apache .htaccess mod-rewrite url-rewriting apache2


【解决方案1】:

您可以在您的站点根目录 .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]

【讨论】:

  • 它显示 404 not found 但文件 /user/blah 实际上是一个有效文件
  • /user/blah 是有效文件吗?我认为这是一个有效的目录。请澄清。
  • 对不起,这是文件,不是目录
  • 当我尝试去/john/blah时它仍然显示404页面
  • 您能否提供完整的 .htaccess,以便我对其进行调查,然后提出可行的解决方案。
猜你喜欢
  • 2014-06-18
  • 2014-02-07
  • 2012-04-30
  • 1970-01-01
  • 2019-11-24
  • 2012-01-26
  • 1970-01-01
  • 2012-05-06
  • 2014-09-11
相关资源
最近更新 更多