【问题标题】:htaccess rewrite rule for admin panel管理面板的 htaccess 重写规则
【发布时间】:2013-02-06 18:11:11
【问题描述】:

我的网络应用程序中有这样的 url 模式“www.mysitename.com/foldername/controller/method”。 并且所有请求的页面首先被重定向到根文件夹上的 index.php,然后它处理请求的页面。 但是每当我进入管理面板 (www.mysitename.com/admin) 时,url 都会将“admin”作为文件夹名称,它会显示管理员的 index.php,(这在这种方式)然后使用 controllernamemethodname 它不会通过管理文件夹的 index.php 重定向。

每当我访问“管理员”时,我都想要这种模式的网址“www.mysitename.com/admin/foldername/controller/method”。以及所有 通过 admin 请求的页面应首先重定向到 admin 文件夹中的 index.php。 这是我的 htaccess 文件 -

    Options -Indexes 
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

【问题讨论】:

  • 那么,任何带有/admin/ 的东西都应该转到/admin/index.php,而不是/index.php
  • 是的,没错。任何带有/admin 的东西都应该到 admin 目录下的 index.php 中。

标签: php .htaccess url-rewriting url-routing


【解决方案1】:

您的管理目录中有.htaccess 文件,还是只有一个在根目录中?您需要在每个目录中都有一个。

我刚刚在/htdocs/pro 中使用您的.htaccess/htdocs/pro/admin 中的相同.htaccess 对此进行了测试,它的工作原理与您解释的完全一样。

/htdocs/pro/.htaccess 中的根 htaccess

Options -Indexes 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

/htdocs/pro/admin/.htaccess 中的管理员 htaccess

Options -Indexes 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

/htdocs/.htaccess/htdocs/pro/.htaccess 中的单个 htaccess 文件

Options -Indexes
RewriteEngine on

# Change '/pro/' if directory is renamed
RewriteBase /pro/

# Change the '/pro/' portion of the condition if directory is renamed 
RewriteCond %{REQUEST_URI} ^/pro/admin(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ admin/index.php?url=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

【讨论】:

  • 谢谢,这行得通.. 但是如果不将 htaccess 文件放入 admin 目录,还有其他可以同样工作的东西吗?
  • 答案更新为单个.htaccess,涵盖要放置在根目录中的管理员和根目录。它将首先检查是否存在与 admin uri 匹配的内容,如果没有,则假定为 root uri。
  • 我只是在我的本地主机上测试这个。但它重定向到本地服务器的 index.php。我将项目放在 htdocs 中名为 pro 的目录中。
  • 我已更新 .htaccess 文件以在 /htdocs//htdocs/pro/ 中工作
  • 如果您将其重命名为其他名称,您只需更新 .htaccess 文件中第 4、9 和 15 行的 /pro/ 部分。
猜你喜欢
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多