duanqiao1949 2018-10-23 12:03
浏览 71
已采纳

获取htaccess中的当前目录名称并阻止重定向

I have this .htaccess file:

Options +FollowSymLinks
DirectorySlash On
RewriteEngine On

# Remove trailing slash for non directories
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)/$ /mydir%1/$1 [R,L=303]

# Make direct files accessible
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !^base.php.*$
RewriteRule ^([^
]+)|(.*)$ base.php?___ROUTE=$1 [QSA,END]

Which having following redirection behaviors:

  1. ✔️ d.com/mydird.com/mydir/
  2. ✔️ d.com/mydir/nothingrun
  3. ✔️ d.com/mydir/nothing/d.com/mydir/nothing
  4. d.com/mydir/exist_dird.com/mydir/exist_dir/?___ROUTE=exist_dir
  5. d.com/mydir/exist_dir/run
  6. ✔️ d.com/mydir/exist_file.cssrun
  7. ✔️ d.com/mydir/exist_file.css/d.com/mydir/exist_file.css

By run I mean It respond file contents or execute base.php?___ROUTE=$1

Now I have a problem and a question

Problem:

I don't know how to fix redirection number 4.

Number 5 should redirect to d.com/mydir/exist_dir.

And number 4 should execute base.php?___ROUTE=$1 instead of redirection

Question:

How can I use current directory name in .htaccess instead of writing it's name in each project?

I mean /mydir in RewriteRule ^(.*)/$ /mydir%1/$1 [R,L=303]

  • 写回答

1条回答 默认 最新

  • donglue1886 2018-10-25 10:19
    关注

    I did it.

    How to fix redirection on non-directory with ending slash

    First, I've made an ENV Variable for removed trailing slash path then use it in RewriteRule.

    Options +FollowSymLinks
    DirectorySlash On
    RewriteEngine On
    
    SetEnvIf Request_URI ^(.*)/$ CUSTOM_BASE_DIR=$1
    
    # Remove trailing slash for non directories
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)/$ %{ENV:CUSTOM_BASE_DIR} [R,L=303]
    
    # Make direct files accessible
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteCond %{REQUEST_URI} !^base.php.*$
    RewriteRule ^([^
    ]+)|(.*)$ base.php?___ROUTE=$1 [QSA,END]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写