简体   繁体   English

Apache htaccess RewriteRule不起作用

[英]Apache htaccess RewriteRule not working

For some reason, I really cannot get my .htaccess RewriteRule to work. 由于某些原因,我确实无法使.htaccess RewriteRule正常工作。 I tried many things and did my Googling, but failed. 我尝试了很多事情并进行了谷歌搜索,但是失败了。 One possible cause might be I have a pretty old Apache version (but this is out of my control). 一个可能的原因可能是我有一个很旧的Apache版本(但这是我无法控制的)。 Not sure how to retrieve the version, but phpinfo() shows me "Apache API Version 20051115". 不确定如何获取版本,但是phpinfo()向我显示“ Apache API版本20051115”。 It has to work on desktops, Blackberry's and iPads at least. 它必须至少在台式机,Blackberry和iPad上运行。

I want to rewrite this: http://subdomain.domain.com/folder1/?id=1234 to the following: http://othersubdomain.domain.com/?id=1234 where id can be any number (I'm fine with rewriting any parameters, I'm also fine with redirecting to /folder1/?id=1234, the important part is the subdomain and the fact that the parameters are still there). 我要将其重写: http : //subdomain.domain.com/folder1/? id= 1234更改为以下内容: http : //othersubdomain.domain.com/? id= 1234其中id可以是任何数字(我是可以重写任何参数,也可以重定向到/ folder1 /?id = 1234,这很重要,重要的是子域和参数仍然存在的事实。 Important is that I do not get an authentication pop-up (on any of the devices). 重要的是我(在任何设备上)都没有弹出验证窗口。

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

AuthUserFile /home/user/path/to/.htpasswd
AuthGroupFile /dev/null
AuthName "This site requires a valid username and password"
AuthType Basic

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder1.*$ http://othersubdomain.domain.com%{REQUEST_URI}? [redirect=301,last]

<limit GET POST>
require valid-user
</limit>

The only thing that seems to get redirected correctly now on my machine is subdomain.domain.com/folder1/ (without the last / it does not work, or if I add parameters to the end, ie, .../folder1/?id=1234 it also does not work). 现在似乎唯一正确地重定向到我的机器上的东西是subdomain.domain.com/folder1/(没有最后一个/它不起作用,或者如果我在末尾添加参数,即... / folder1 /? id = 1234也不起作用)。

I further experimented with the following: 我进一步尝试了以下方法:

AuthUserFile /home/user/path/to/.htpasswd
AuthGroupFile /dev/null
AuthName "This site requires a valid username and password"
AuthType Basic

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/folder1
RewriteRule .* http://othersubdomain.domain.com/?%{QUERY_STRING} [redirect=301,last]

<limit GET POST>
require valid-user
</limit>

This redirect seems to work, but only after I give the correct authentication user/password, and that is exactly what I want to prevent. 这种重定向似乎有效,但是只有在我提供了正确的身份验证用户/密码之后,这才是我想要避免的。

Tips are more than welcome ... 提示不只是受欢迎...

I think this should work (without any RewriteCond): 我认为这应该工作(没有任何RewriteCond):

RewriteRule ^folder1(.*)$ http://othersubdomain.domain.com$1 [L,QSA,R=301]

QSA flag tell to keep query params. QSA标志告诉保持查询参数。 $1 is replaced with contents matched by ( ). $ 1被替换为()的内容。

PS As far as I know you can use %{...} variables only in RewriteCond. PS据我所知,您只能在RewriteCond中使用%{...}变量。

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

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