jeebee
jeebee

Reputation: 73

Apache htaccess RewriteRule not working

For some reason, I really cannot get my .htaccess RewriteRule to work. 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). Not sure how to retrieve the version, but phpinfo() shows me "Apache API Version 20051115". It has to work on desktops, Blackberry's and iPads at least.

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). 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, i.e., .../folder1/?id=1234 it also does not work).

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 ...

Upvotes: 1

Views: 536

Answers (1)

Vladislav Ross
Vladislav Ross

Reputation: 581

I think this should work (without any RewriteCond):

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

QSA flag tell to keep query params. $1 is replaced with contents matched by ( ).

P.S. As far as I know you can use %{...} variables only in RewriteCond.

Upvotes: 1

Related Questions