.htaccess 重写为相同的别名而无需无限重定向
.htaccess rewrite to same alias without infinite redirects
我有...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
所以,/in
--是-真的--> /login.php
这非常有效。我们都可以从中学习如何做到这一点:
但是,我希望它也能反向工作...
如果有人应该在地址栏中输入 /login.php
,我希望它更改为 /in
。
同样,/login.php
--重写为--> /in
从 到另一个问题,我想使用 REQUEST_URI
为任何事情做好准备。所以,我的 .htaccess
文件 开始 这个...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
这也很好用。
但是现在,我想为 /in <--> /login.php
添加这条规则(我的问题在这里),这就是 / <--> /index.php
如何与 .htaccess
(v2) 一起工作。所以,我采用了设置并添加了第二条规则...
| .htaccess : (v3) —不工作!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...但是 /in
和 /login.php
都会导致无限重定向循环。
这样做的正确方法是什么,仍然使用 REQUEST_URI
,并且仍然 重写规则(对于 index.php
和 login.php
)?
这些问题没有帮助:
- Rewrite rule to hide folder, doesn't work right without trailing slash
- 这与尾部斜线无关
- Allow multiple IPs to access Wordpress Site Admin via .htaccess
- 这与基于 IP 的访问无关
- 这不是关于
https
vs http
- Rewrite-rules issues : .htaccess
- 这与清理 URL
中的 GET
数组无关
- apache htaccess rewrite with alias
- 这不是重写host/domain,从而保留路径
- rewrite htaccess causes infinite loop?
- 这与
www
子域重写无关
- .htaccess rewrite page with alias
- 这不是关于重写“漂亮”URLs,也不是关于如何在 WordPress 中使用 slug 设置
- Htaccess alias or rewrite confusion
- 这不是简单地让多个规则具有相同的目的地
- htaccess rewrite to include #!
- 我不是要重写
#!
重定向循环的原因是在删除 index.php
的第一个重定向规则之前缺少 RewriteCond %{ENV:REDIRECT_STATUS} ^$
。请记住,RewriteCond
仅适用于紧接的下一个 RewriteRule
。
建议的 .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
它不会导致重定向循环,因为在第一次重写/login.php
之后,变量REDIRECT_STATUS
将变为200
,然后RewriteCond %{ENV:REDIRECT_STATUS} ^$
将停止重定向循环。
感谢网友帮助答对,我发现...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...不进去.htaccess
只有一次,而是每次上线前...
RewriteCond %{REQUEST_URI} ...
我有...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
所以,/in
--是-真的--> /login.php
这非常有效。我们都可以从中学习如何做到这一点:
但是,我希望它也能反向工作...
如果有人应该在地址栏中输入 /login.php
,我希望它更改为 /in
。
同样,/login.php
--重写为--> /in
从 REQUEST_URI
为任何事情做好准备。所以,我的 .htaccess
文件 开始 这个...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
这也很好用。
但是现在,我想为 /in <--> /login.php
添加这条规则(我的问题在这里),这就是 / <--> /index.php
如何与 .htaccess
(v2) 一起工作。所以,我采用了设置并添加了第二条规则...
| .htaccess : (v3) —不工作!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...但是 /in
和 /login.php
都会导致无限重定向循环。
这样做的正确方法是什么,仍然使用 REQUEST_URI
,并且仍然 重写规则(对于 index.php
和 login.php
)?
这些问题没有帮助:
- Rewrite rule to hide folder, doesn't work right without trailing slash
- 这与尾部斜线无关
- Allow multiple IPs to access Wordpress Site Admin via .htaccess
- 这与基于 IP 的访问无关
- 这不是关于
https
vshttp
- 这不是关于
- Rewrite-rules issues : .htaccess
- 这与清理 URL 中的
GET
数组无关 - apache htaccess rewrite with alias
- 这不是重写host/domain,从而保留路径
- rewrite htaccess causes infinite loop?
- 这与
www
子域重写无关
- 这与
- .htaccess rewrite page with alias
- 这不是关于重写“漂亮”URLs,也不是关于如何在 WordPress 中使用 slug 设置
- Htaccess alias or rewrite confusion
- 这不是简单地让多个规则具有相同的目的地
- htaccess rewrite to include #!
- 我不是要重写
#!
- 我不是要重写
重定向循环的原因是在删除 index.php
的第一个重定向规则之前缺少 RewriteCond %{ENV:REDIRECT_STATUS} ^$
。请记住,RewriteCond
仅适用于紧接的下一个 RewriteRule
。
建议的 .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
它不会导致重定向循环,因为在第一次重写/login.php
之后,变量REDIRECT_STATUS
将变为200
,然后RewriteCond %{ENV:REDIRECT_STATUS} ^$
将停止重定向循环。
感谢网友帮助答对,我发现...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...不进去.htaccess
只有一次,而是每次上线前...
RewriteCond %{REQUEST_URI} ...