【问题标题】:Rewrite htaccess from specific subfolder从特定子文件夹重写 htaccess
【发布时间】:2013-12-31 22:17:57
【问题描述】:

我有一个 htaccess 设置,已经重写所有对 index.php 的请求。但现在我有一个条件,我想重写(我不知道该怎么做或选择哪个)a subfolder /admin to index.php?route=admin/login,是的准确的网址

期待

www.domain.com/admin

改写为

www.domain.com/index.php?route=admin/login

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/install(.*)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
#########################################

##I expect the line below will go to the url , but when i enter the subfolder , the page remain at the index
RewriteRule ^admin/(.*)$ /$1?route=admin/login [R=301,L]

【问题讨论】:

  • /admin/ 文件夹中有任何 .htaccess 吗?
  • @anubhava 是的,我可以拥有

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

试着把它放在/admin/.htaccess:

RewriteEngine On
RewriteBase /admin/

RewriteRule ^$ /index.php?route=admin/login [R=301,QSA,L]

【讨论】:

    【解决方案2】:

    从 www.domain.com/admin 重写为管理员登录

    RewriteRule ^admin$ /index.php?route=admin/login [R=301,L]
    

    重写其他请求,例如:

    www.domain.com/admin/list_users

    www.domain.com/admin/send_mail

    等等

    RewriteRule ^admin/(.*)$ /index.php?route=admin/$1 [R=301,L]
    

    更新。

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/install(.*)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php [L]
    #########################################
    
    ##I expect the line below will go to the url , but when i enter the subfolder , the page remain at the index
    #RewriteRule ^admin/(.*)$ /$1?route=admin/login [R=301,L]
    

    AND 从 php 文件改为 $_GET['route'] 使用 $_SERVER['REQUEST_URI']

    或 UPD domain.com/admin/login

    RewriteEngine On
    RewriteRule ^admin/(.*)$ /index.php?route=admin/$1 [R=301,L]
    
    RewriteCond %{REQUEST_URI} !^/install(.*)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php [L]
    #########################################
    

    【讨论】:

    • 你有 RewriteRule 。 index.php [L]/
    • 仔细阅读我的问题
    • 还是一样,我应该去重定向吗?
    • "redirect" 是发送到不同的域(R=302 是默认的,R=301 可以明确给出)。实际上,任何提供 http: 和域都是重定向,即使它指向同一个域。 “重写”通过不提供 http: 和域(R=200 是默认值)保持在当前域内。
    猜你喜欢
    • 2017-09-08
    • 2015-09-27
    • 2021-08-05
    • 1970-01-01
    • 2012-12-12
    • 2016-05-19
    • 1970-01-01
    • 2016-12-21
    • 2012-11-22
    相关资源
    最近更新 更多