Forum Moderators: phranque

Message Too Old, No Replies

Help mod rewrite

         

triplee23

6:29 am on Dec 5, 2011 (gmt 0)

10+ Year Member



Hi, could anyone help me with my rewrite, I keep getting an infinite loop.

I want all trafic to subdomain.domain.com to be redirected to subdomain.domain.com/wordpress.

RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
RewriteRule ^/?(.*) [subdomain.domain.com...] [L]

Thanks in advance!

triplee23

6:33 am on Dec 5, 2011 (gmt 0)

10+ Year Member



Something happened to the code

Hi, could anyone help me with my rewrite, I keep getting an infinite loop.

I want all trafic to subdomain.domain.com to be redirected to subdomain.domain.com/wordpress.

RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]

RewriteRule ^/?(.*) http://subdomain.domain.com/wordpress/$1 [L]


Thanks in advance!

incrediBILL

7:06 am on Dec 5, 2011 (gmt 0)

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



RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]

The problem here is that HTTP_HOST will always equal subdomain.domain.com which is why it's looping.

Try something like this instead:
RewriteCond %{REQUEST_URI} !^/wordpress/.*

The idea here is if /wordpress/ is NOT in the REQUEST_URI, then then add it.

Should do the trick.

triplee23

8:58 am on Dec 5, 2011 (gmt 0)

10+ Year Member



Thanks for your help, I was also thinking this was the problem but my regex knowledge is limited so I was unsure how to fix it.

One more question though, will this RewriteCond be true for all subdomains? How do I write the code to only be valid for one specific subdomain.

For ex.

subdomain1.domain.com --> subdomain.domain.com/wordpress/

subdomain2.domain.com --> do nothing

How do I add to the regex that the URL also must contain the word "subdomain1" (and not have the wordpress folder)?

Thanks again.

triplee23

8:59 am on Dec 5, 2011 (gmt 0)

10+ Year Member



Typo

Example should be:

subdomain1.domain.com --> subdomain1.domain.com/wordpress/

lucy24

10:04 am on Dec 5, 2011 (gmt 0)

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



How do I write the code to only be valid for one specific subdomain.

For ex.

subdomain1.domain.com --> subdomain.domain.com/wordpress/

subdomain2.domain.com --> do nothing

How do I add to the regex that the URL also must contain the word "subdomain1"

You've already done that part. It either is or is not "subdomain.domain.com". You don't have any wild card, and you want to keep it that way.

(and not have the wordpress folder)?

You need a second Condition, as noted above:

The idea here is if /wordpress/ is NOT in the REQUEST_URI, then then add it.


(Bill!? What on earth are you doing here? Did everyone go on vacation and leave you to hold down the fort?)

triplee23

8:59 pm on Dec 5, 2011 (gmt 0)

10+ Year Member



Perfect, it's now working as expected with the following code:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain.com\z$ [NC]
RewriteCond %{REQUEST_URI} !^/wordpress/.* [NC]
RewriteRule ^/?(.*) http://subdomain.domain.com/wordpress/$1 [L]


Thanks all!

lucy24

11:01 pm on Dec 5, 2011 (gmt 0)

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



What's the escaped z in the first Condition? What you should escape is the two literal periods. Here they aren't crucial, because a request for "subdomainzdomainxcom" would never reach you, but you should get in the habit anyway.

Note that if you're neither capturing nor anchoring, you don't need the .* at the end of the second Condition. It won't do any particular harm, since mod_rewrite will simply gulp it down and then burp it out again. But again, it's better not to get in the .* habit except when you really need it for capturing the end of a request.

It's a good thing you made the leading slash optional in your Rule, because otherwise it would always fail. (Unless you get a malformed request with // after the domain name.) The initial slash is built into mod_rewrite.

:: wait ::
:: stop ::
:: rewind ::

Last time I looked, the complete protocol-plus-domain in the Target would automatically change it from a Rewrite to a Redirect-- but it would default to a 302 redirect, which is almost never what anyone wants. When you try it out, do you get a rewrite or a redirect?

triplee23

6:47 am on Dec 6, 2011 (gmt 0)

10+ Year Member



Thanks for all you valuable insight. My knowledge on rewrite_mod is limited so I am not sure I understand all your arguments.

For.ex. could you explain this a lttle more, I don't understand:

What you should escape is the two literal periods. Here they aren't crucial, because a request for "subdomainzdomainxcom" would never reach you, but you should get in the habit anyway.


The /s was a mistake from early testing. I have changed my code to this:


RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/wordpress/ [NC]
RewriteRule ^/?(.*) http://subdomain.domain.com/wordpress/$1 [R=301,L]


I was getting a combination of redirects and rewites, therefore I added R=301 as you requested (if I understood you correctly).

Is the code now optimal do you think?

triplee23

1:20 pm on Dec 6, 2011 (gmt 0)

10+ Year Member



I am now running the apache with the code above. I have noticed that a request for subdomain.domain.com/wordpress takes much longer time than a request for subdomain.domain.com. A request for subdomain.domain.com/wordpress/ is also returned as fast as expected, it therefore has to do with the last "/".

Is there anything in my code giving this result.

It seem that the code is not optimal when the user requests subdomain.domain.com/wordpress directly. I will take a look in my log file when I get home.

Any ideas?

lucy24

12:00 am on Dec 7, 2011 (gmt 0)

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



By default, a trailing slash means a directory. It may help to walk through what would happen if you had no htaccess at all, just the built-in things that all shared hosts serve up. And if it doesn't help you it will probably help me, so all is well anyway. In what follows, "the server" means Apache and its assorted modules, and we'll assume your files end in .html

www.example.com/blahblah.html
>> DNS finds example.com, server finds filename.html, you're there

www.example.com/blahblah/
>>> server looks for directory /blahblah/ and, if found, looks for file /blahblah/index.html or similar. Shared hosts have a list of potential index names to check; if it's your own server, it will be in the config file. If found, server takes you there. If not, it checks whether auto-indexing is enabled. If yes, it makes up an index. If no, user gets a 403. (Yes, the very same 403 that evil robots get. That's why your custom 403 page should be user-friendly.)

The same thing happens if the request is for the domain name alone; it just happens one directory sooner. Requests in the form "www.example.com" without trailing slash will not occur, because the user's browser itself appends the trailing slash.

www.example.com/blahblah
>> now there is a preliminary stage where server looks to see if you have a directory named /blahblah. If yes, it applies the DirectorySlashRedirect (possibly not spelled exactly like that, but it's a real Apache function). And then proceed as above. If no, there may be a further automatic step if-and-only-if mod_negotiation is present and MultiViews is enabled: the server will then look for files named /blahblah.html or /blahblah.some-other-extension. If it finds one, it will take you there. If it finds several, it may offer the user a choice.

This final step-- the one where you're dealing with requests for a "naked" url-- is where your htaccess is most likely to get involved.

triplee23

10:29 am on Apr 30, 2012 (gmt 0)

10+ Year Member


I have now been running my rewritecond successfully for a while but would like to add another rewrite, I am as stated above not very knowledgable on regex som I hope I can get help again. I am know running two wordpress blogs and would like the rewrite to work for both blogs.

Here is my current code:

[code]RewriteEngine On
RewriteCond %{HTTP_HOST} ^user1.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/wordpress.* [NC]
RewriteRule ^/?(.*) http://user1.domain.com/wordpress/$1 [R=301,L][/code]

[code]RewriteEngine On
RewriteCond %{HTTP_HOST} ^user2.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/wordpress.* [NC]
RewriteRule ^/?(.*) http://user2.domain.com/wordpress/$1 [R=301,L][/code]

The problem is that, when requesting user2.domain.com, the result is always being sent to user1.domain.com

Could someone help me with the code so that the apache server can distinguish between user1 and user2 in the second RewriteCond sentence? I guess I have to add some wildcards and user1 or user2 to the RewriteCond?

lucy24

4:36 pm on Apr 30, 2012 (gmt 0)

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



Ouch. Did your cat step on the Disable Code button?

As written:

Rule 1: If request begins with a slash-- or doesn't begin with a slash-- optionally followed by more stuff... OK, I see your thinking but it isn't necessary. The leading slash either is or is not present, depending on whether you're on shared hosting. So a simple (.*) will do.

Condition 1: If host is exactly "user1.domain.com" or possibly "user1xdomainxcom" or "user11domain1com"... (Escaping literal periods is not as crucial here as some places, because a malformed request simply wouldn't reach you, but it is much better to get in the habit of escaping all periods in all patterns.)

Hmm. Looking back over this thread I see that I said the exact same thing back around last December.

Condition 2: If requested file does not begin with "/wordpress" including leading slash... I should think this rule would always fail, unless there's a REQUEST_URI loophole that ignores leading slashes. You don't need the trailing .* since you are neither capturing nor anchoring.

The problem is that, when requesting user2.domain.com, the result is always being sent to user1.domain.com


Both rules start with a positive Condition-- HTTP_HOST is such-and-such-- so unless you've got an overlooked typo, there's nothing there to prompt a redirect to a different subdomain. There has to be something else going on. If you comment-out both Redirects, does anything change?

Where is the htaccess located? It has to be in a directory where both subdomains will "see" it. The most common arrangement is to have physical directories inside your top-level directory mapping to subdomains, so you get:

User requests
www.example.com
subdomain1.example.com
subdomain2.example.com

User gets content of
blahblah/userspace/example.com
blahblah/userspace/example.com/subdomain1
blahblah/userspace/example.com/subdomain2

(the blahblah is all the preliminary levels from the config file on down to your own userspace, which itself may contain more than one domain)

g1smd

11:46 pm on Apr 30, 2012 (gmt 0)

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



Fix all the typos and add the missing escaping in the code and clear the browser cache.