.htaccess URL重写到其他文件夹和文件以满足特定请求
我的本地LAMP上有以下目录结构:.htaccess URL重写到其他文件夹和文件以满足特定请求,.htaccess,mod-rewrite,redirect,url-rewriting,apache2,.htaccess,Mod Rewrite,Redirect,Url Rewriting,Apache2,我的本地LAMP上有以下目录结构: 万维网 基本的 应用程序 router.php 公开的 index.html .htaccess 我可以使用localhost/basic/public/访问我的索引页,并且我不使用任何虚拟主机 现在我试着做两件事: 1:要访问主页,URL应该看起来像localhost/basic/,而不是localhost/basic/ 2:所有对/api/的请求都应重定向到router.php。例如,如果我发出一个像/api/user/login这样的
- 万维网
- 基本的
- 应用程序
- router.php
- 公开的
- index.html
- .htaccess
- 应用程序
- 基本的
localhost/basic/public/
访问我的索引页,并且我不使用任何虚拟主机
现在我试着做两件事:
1:要访问主页,URL应该看起来像localhost/basic/
,而不是localhost/basic/
2:所有对/api/
的请求都应重定向到router.php
。例如,如果我发出一个像/api/user/login
这样的请求,它应该转到app/router.php
,在那里我可以根据api请求执行特定的代码
为了实现这一点,我尝试在.htaccess文件中执行以下操作,但不起作用:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule . ../app/router.php [NC,L]
此外,我不确定是否可以使用
重写规则中的相对路径来总结,您需要:
- 有一种特殊情况:如果url以/basic/api开头,您希望它转到router.php页面
- 默认情况下,应该公开搜索不是文件的url
此外,您可以将转到/basic/public/something的请求重定向到/basic/something
将其放入基本文件夹中的.htaccess文件中:
RewriteEngine On
RewriteBase /preview/
#Special case: api requests
RewriteRule ^api/ app/router.php [L]
#Default case: all public files
#Alternativelly use RewriteCond %{REQUEST_URI} !/basic/public
RewriteCond %{REQUEST_URI} !/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
#Optionally prevent both localhost/basic/public/file and localhost/basic/file
#outputting the same
#Using THE_REQUEST trick to only match external requests
RewriteCond %{THE_REQUEST} ^(POST|GET)\ /basic/public/
RewriteRule ^public/(.*)$ $1 [R,L]
这将执行以下操作:
localhost/basic/api/user/something
内部重写为localhost/basic/app/router.php
localhost/basic/something
内部重写为localhost/basic/public/something
localhost/basic/public/something
从外部重定向到localhost/basic/something
(然后在内部重写)
行为的屏幕广播:总结一下,您需要:
- 有一种特殊情况:如果url以/basic/api开头,您希望它转到router.php页面
- 默认情况下,应该公开搜索不是文件的url
此外,您可以将转到/basic/public/something的请求重定向到/basic/something
将其放入基本文件夹中的.htaccess文件中:
RewriteEngine On
RewriteBase /preview/
#Special case: api requests
RewriteRule ^api/ app/router.php [L]
#Default case: all public files
#Alternativelly use RewriteCond %{REQUEST_URI} !/basic/public
RewriteCond %{REQUEST_URI} !/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
#Optionally prevent both localhost/basic/public/file and localhost/basic/file
#outputting the same
#Using THE_REQUEST trick to only match external requests
RewriteCond %{THE_REQUEST} ^(POST|GET)\ /basic/public/
RewriteRule ^public/(.*)$ $1 [R,L]
这将执行以下操作:
localhost/basic/api/user/something
内部重写为localhost/basic/app/router.php
localhost/basic/something
内部重写为localhost/basic/public/something
localhost/basic/public/something
从外部重定向到localhost/basic/something
(然后在内部重写)
行为视频:非常感谢您提供的详细答案。公共部分现在工作正常,但我对api有问题。当我转到localhost/basic/api/test
而不是请求/basic/app/router.php
时,它正在请求/basic/public/app/router.php
。在api情况下,公众不应在场。知道为什么吗?在第二条规则中,我使用了%{REQUEST\u URI}
而不是%{REQUEST\u FILENAME}
。这是不正确的,他把自己丢到了耻辱的角落。假设router.php确实存在于app文件夹下,如果您更改它,它应该可以工作。很抱歉回复太晚。现在一切都很好。非常感谢你的帮助。非常感谢你的详细回答。公共部分现在工作正常,但我对api有问题。当我转到localhost/basic/api/test
而不是请求/basic/app/router.php
时,它正在请求/basic/public/app/router.php
。在api情况下,公众不应在场。知道为什么吗?在第二条规则中,我使用了%{REQUEST\u URI}
而不是%{REQUEST\u FILENAME}
。这是不正确的,他把自己丢到了耻辱的角落。假设router.php确实存在于app文件夹下,如果您更改它,它应该可以工作。很抱歉回复太晚。现在一切都很好。非常感谢你的帮助。