Forum Moderators: phranque

Message Too Old, No Replies

Help with htaccess

htaccess

         

buildersweb

3:26 pm on Aug 5, 2014 (gmt 0)

10+ Year Member



Hello

I try to add a redirect like this

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.com [OR]
RewriteCond %{HTTP_HOST} ^www.test.com
RewriteRule (.*) http://www.test/subfolder/$1 [R=301,L]

But as a result i get a loop like this

http://www.test/subfolder/subfolder/subfolder/subfolder/subfolder/


However at this tool the code worked fine
http://htaccess.madewithlove.be/

What is wrong ?

Thanks

[edited by: incrediBILL at 4:05 pm (utc) on Aug 5, 2014]

[edited by: phranque at 1:53 am (utc) on Aug 6, 2014]
[edit reason] unlinked URLs [/edit]

not2easy

4:58 pm on Aug 5, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The best I can figure out, you are redirecting to "http://www.example/subfolder/$1" without specifying a domain. Things that seem to work in specific tools, sometimes can only work in that environment. When you try to use it on your domain it does not work because it is in an entirely different environment. I am pretty sure that there are other lines in your htaccess file. (There should be)

Your target is designed to append the request that followed the pattern in the original request: subfolder/$1 but the query is not captured in the rule, so it won't be appended to the redirect.

Let's start with what you are trying to do. Then you can get help for how to do it. This looks like you are trying to handle the www/non-www canonical issue in the same rewrite that sends visitors to a subdirectory and adds on the page requested originally but I'm not too sure. To prevent accidental linking (that makes the question hard to read), we use "example.com" because that won't cause accidental links. Now, if you can put it in simple terms using example.com, can you please clarify what you want this rule to do?

lucy24

7:06 pm on Aug 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Time for a chorus of

I really hate this damned machine
I wish that they would sell it
It never does quite what I want
But only what I tell it.


The rule is doing exactly what it has been told to do: add /subfolder/ to the front of the request. It will keep redirecting forever unless you add a Condition to exclude requests that have already been redirected:

RewriteCond %{REQUEST_URI} !subfolder

buildersweb

8:43 am on Aug 6, 2014 (gmt 0)

10+ Year Member



I want to achieve the following

the example.com to redirect at www.example.com/subfolder/
the www.example.com to redirect at www.example.com/subfolder/

This is not working

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [OR]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://www.example.com/subfolder/$1 [R=301,L]

wilderness

10:59 am on Aug 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Pretty sure this is what lucy explained and provided.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [OR]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteCond %{REQUEST_URI} !subfolder
RewriteRule (.*) http://www.example.com/subfolder/$1 [R=301,L]

buildersweb

11:48 am on Aug 6, 2014 (gmt 0)

10+ Year Member



That worked !

Thanks

lucy24

7:55 pm on Aug 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^example.com [OR]
RewriteCond %{HTTP_HOST} ^www.example.com

=
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com


That's assuming you have subdomains that need to be excluded. If not, just example\.com without opening anchor.

wilderness forgot to escape the periods ;)

wilderness

8:06 pm on Aug 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm getting old and damned fast ;)

Just did a copy and paste and inserted your line.