简体   繁体   English

htaccess 从顶部重定向到子目录

[英]htaccess redirect from top to subdirectory

I have a web server with the following (simplified) layout:我有一个具有以下(简化)布局的 Web 服务器:

/
    www/  # will hold html and PHP files (web content)
    doc/  # some documentation
    lib/  # some libraries

I would like to use htaccess to "redirect" every request to www.mydomain.com/page to www.mydomain.com/www/page.php .我想使用htaccess将每个请求“重定向”到www.mydomain.com/pagewww.mydomain.com/www/page.php

I have tried the following:我尝试了以下方法:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)$ www/$1.php

But it produces a 500 Internal Server Error .但它会产生500 Internal Server Error

For debugging purposes, I created a www/test.php page which echoes every GET variable, and I modified my .htaccess :出于调试目的,我创建了一个www/test.php页面,该页面响应每个 GET 变量,并修改了我的.htaccess

RewriteRule ^(.*)$ www/test.php?page=$1

just to check whether I'm matching the right things.只是为了检查我是否匹配正确的东西。 The expected behaviour when performing a request against www.mydomain.com/somepage would be page=somepage , instead I get page=www/get.php .www.mydomain.com/somepage执行请求时的预期行为将是page=somepage ,而不是我得到page=www/get.php

Why this behaviour?为什么会有这种行为? How can I accomplish what I need?我怎样才能完成我所需要的?

You have to exclude the path you are rewriting to :您必须排除要重写的路径:

RewriteRule ^((?!www).*)$ www/test.php?page=$1 [NC,L]

otherwise you will get an infinite loop error because www/test.php also matches the rewrite pattern (.*)否则你会得到一个无限循环错误,因为www/test.php也匹配重写模式(.*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM