homepage Welcome to WebmasterWorld Guest from 54.196.198.213
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 47 message thread spans 2 pages: 47 ( [1] 2 > >     
non-www to www WITH subdomains
need help with correct configuration
mihomes




msg:4588081
 7:41 am on Jun 27, 2013 (gmt 0)

I am using the following for non-www to www on my sites :


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


I want to add a subdomain such as blog.example.com. I have two issues to figure out :

1 - Exclude the subdomain from the above rewrite as it currently rewrites blog.example.com to www.example.com/blog/

2 - The actual subdomain location is /blog/, but that should never be accessible to people or engines. So, /blog/* should always be blog.example.com/*

I'm guessing this is a pretty common thing, but I have not used subdomains in ages. Any recommendations would be great... I would like to keep the original rewrite intact since it is the most complete non-www to www I was able to come across to date.

thanks

 

lucy24




msg:4588089
 8:05 am on Jun 27, 2013 (gmt 0)

When you say /blog/* do you mean /blog/.* ? I hope that was just shorthand for the sake of posting :) (Asterisk as wild card, rather than asterisk as RegEx meaning "zero or more".)

You can either put a supplementary RewriteCond before the current one to say
RewriteCond %{HTTP_HOST} !blog\.
or you can collapse it all into the existing RewriteCond by changing it to
((www|blog)\.example\.com)

Do you envision any wrong requests for the blog? Things like www.blog.example.com? If so, it will need a separate RewriteRule whose conditions say (in English) "request contains 'blog' but is not exactly 'blog.example.com'". Put this one immediately before the general domain-name redirect... and if you are feeling brave you can throw in a [S=1] flag as an alternative to slathering on extra conditions in the very last redirect. If you use the flag, BE SURE to put a comment to remind yourself that this pair of rules MUST stay together in the same order.

You'll also need a redirect-- no condition needed-- that says simply
RewriteRule ^blog/(.*) http:/ /blog.example.com/$1 [R=301,L]
That's an "insurance redirect". If all links are correctly worded and all rewrites happen as intended, you will never really get a request in this form. This comes before the (sub)domain-name redirect(s), because the (sub)domain name doesn't matter; the request would be wrong in any case.

mihomes




msg:4588112
 9:27 am on Jun 27, 2013 (gmt 0)

Thanks for the reply Lucy. Yes, I was using the * as shorthand.

I actually tried your recommendation before posting :


Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


and it did not work at all. That is what led me to post tonight. My thought was 'if NOT www.example.com* OR blog.example.com* then redirect to the www version. Maybe I am too tired, but I don't see anything wrong with it, but it doesn't work.

Thanks for pointing out www.blog.example.com as I didn't even think about that... I want to take care of all possibilities to prevent any dupe urls.

mihomes




msg:4588128
 10:10 am on Jun 27, 2013 (gmt 0)

Getting different results between firefox and ie10 which makes no sense at all since this is all server side. They each act differently with the same code below.


RewriteCond %{HTTP_HOST} ^(www\.blog\.example\.com)?$
RewriteRule (.*) http://blog.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Looks like it is working with one problem :

Entering www.blog.example.com* always redirects to blog.example.com/blog/*

For whatever reason it adds/redirects to the folder location.

mihomes




msg:4588157
 11:56 am on Jun 27, 2013 (gmt 0)

Okay, thanks to phranque for pointing me to another thread that included some code by jdMorgan... I came up with this :


Options +SymLinksIfOwnerMatch

# BEGIN old page redirects
Redirect 301 /test.htm http://www.example.com/testing.htm

# BEGIN errors
ErrorDocument 400 /error.htm
ErrorDocument 401 /error.htm
ErrorDocument 403 /error.htm
ErrorDocument 404 /error.htm
ErrorDocument 500 /error.htm

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteEngine On
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

# non-www to www if not www or blog
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# BEGIN defaults
DefaultLanguage en-US
AddDefaultCharset UTF-8
AddType 'text/css; charset=UTF-8' css
AddType 'application/javascript; charset=UTF-8' js

# BEGIN expires rules
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(jpe?g|gif|png|ico|pdf|swf|flv)$">
ExpiresDefault "access plus 20 days"
Header append Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(js|css|txt)$">
ExpiresDefault "access plus 10 days"
Header append Cache-Control "private, must-revalidate"
</FilesMatch>
<FilesMatch "\.(html?|php)$">
ExpiresDefault "access plus 2 days"
Header append Cache-Control "private, must-revalidate"
</FilesMatch>
ExpiresDefault "access plus 1 hours"
</IfModule>

# BEGIN compress files
<IfModule mod_deflate.c>
<FilesMatch "\.(html?|php|js|css|rss|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>


Only problem - it appears to work in all cases I have tried in Firefox yet does not work properly in IE10...how is that even possible since this is server side?

Help from greater minds?

phranque




msg:4588161
 12:17 pm on Jun 27, 2013 (gmt 0)

mihomes is referring to this thread.

rewrite subdomain to folder:
http://www.webmasterworld.com/apache/3404559.htm [webmasterworld.com]

phranque




msg:4588162
 12:26 pm on Jun 27, 2013 (gmt 0)

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>


you need to adapt that code for your application.

in your case you are internally rewriting <subdomain>.example.com/<URLpath> to example.com/<subdomain/<URLpath> and externally redirecting client requests for example.com/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>

if "<subdomain>" is only "blog" then you will have to adjust the RewriteRule Substitution strings accordingly.

phranque




msg:4588167
 1:00 pm on Jun 27, 2013 (gmt 0)

Only problem - it appears to work in all cases I have tried in Firefox yet does not work properly in IE10...how is that even possible since this is server side?


did you clear browser cache before each test?

mihomes




msg:4588202
 3:36 pm on Jun 27, 2013 (gmt 0)

I used the /subs/ method to store all subdomain directories in as mentioned on the other post - it is applied correctly.

example.com - www.example.com - correct

example.com/test/test.htm - example.com/test/test.htm - wrong

example.com/subs/ - www.example.com/subs/ - correct

example.com/subs/blog/ - blog.example.com - correct

example.com/subs/blog/test.htm - example.com/subs/blog/test.htm - wrong

Everything that I above that is wrong works perfectly fine in FF. In IE10 I get the results above.

Is there a conflict with having the root domain password protected with IE? I have a password on this particular domain as I use it to test 'online' content and do not allow outside access. That is the only possible thing I can think of at the moment unless headers are being returned wrong for IE.

lucy24




msg:4588306
 8:54 pm on Jun 27, 2013 (gmt 0)

A post by jdmorgan has an internal rewrite BEFORE an external redirect? That can only be due to rare and specific circumstances, not applicable to most situations. (The obvious case is when you're rewriting to a php script that will then issue any required redirects or 400-class errors.)

Matter of fact, the <IfModule> envelopes strongly suggest there's other stuff going on. You'd never say this in an htaccess file written for a specific site at a specific time.

Everything ... that is wrong works perfectly fine in FF. In IE10 I get the results above.

Browser variation does point to a cache problem. If you're testing anywhere other than your live site, you can save yourself some aggravation by adding lines such as

ExpiresActive On
ExpiresByType text/html "access"

adding extensions as appropriate. Obviously you can't do this on a live site. But if your browser honors the directive, it means that every single time you look at the page, the file will be re-loaded.

mihomes




msg:4589547
 10:40 am on Jul 2, 2013 (gmt 0)

Still having the same issue... cache is fine and I even managed to try on other computers with IE10 with the same results.

I also removed the password protection on the site temporarily to rule out it being a problem - same results either way.

Anyone want to take a shot at this one - it just does not work correctly in IE, but does in others. Lucy I added your expires in for good measure and it made no change.


Options +SymLinksIfOwnerMatch
RewriteEngine On

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]

# non-www to www if not www or blog
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# BEGIN expires rules
ExpiresActive On
ExpiresByType text/html "access"

phranque




msg:4589669
 7:46 pm on Jul 2, 2013 (gmt 0)

this msdn page is for IE9 but i'm assuming it's the same for IE10.

Using Windows Internet Explorer Developer Tools Network Capture (Internet Explorer):
http://msdn.microsoft.com/en-us/library/ie/gg130952(v=vs.85).aspx [msdn.microsoft.com]

please find out what the response status chain is for your requests from IE10 and report back.

lucy24




msg:4589684
 8:21 pm on Jul 2, 2013 (gmt 0)

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
<snip>
# non-www to www if not www or blog


The www redirect needs to be BEFORE all internal rewrites. This principle applies to all redirects and rewrites all the time, unless there is a clear and specific reason to use a different ordering.

mihomes




msg:4589719
 10:12 pm on Jul 2, 2013 (gmt 0)

Phranque - you solved the problem. Everything is happening correctly it is just that IE does not update the url in the browser if the page is not found. Since I was using this on a test domain I had not created any page structure for the most part so when there was a 404 encountered it just returned the 404 without updating the url.

Lucy I also made the change to have all redirects before the rewrite.

The only thing I can complain about now is this url format :

www.blog.test.com

redirects to www.test.com/subs/blog/ then

redirects to blog.test.com

because it has two redirects in one. I don't think there is a way around that one though and doubt it would even come up much, but at least it works if it should. Thanks for everyones help on this. Guess I should have looked at the responses in the first place just assumed the browser url would always update accordingly.

If anything else can/could/should be optimized please chime in. Have a great week everyone!

phranque




msg:4589723
 10:39 pm on Jul 2, 2013 (gmt 0)

it looks like your old rules might have done that.

clear your browser cache and any proxy caches and try again.
post your current rewrite/redirect rulesets and the response chain including status codes and Location: headers.

mihomes




msg:4589732
 11:41 pm on Jul 2, 2013 (gmt 0)

Here it is :


Options +SymLinksIfOwnerMatch
RewriteEngine On

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

# non-www to www if not www or blog
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]

# BEGIN expires rules
ExpiresActive On
ExpiresByType text/html "access"


When accessing www.blog.example.com :


URLMethodResultTypeReceivedTakenInitiatorWaitııStartııRequestııResponseııCache readııGapıı
http://www.blog.example.com/GET301text/html332 B2.32 snavigate018778002996
http://www.example.com/subs/blog/GET301text/html324 B63 msnavigate2324063002933
http://blog.example.com/GET200text/html341 B2.80 snavigate23871567800125

KeyValue
ResponseHTTP/1.1 301 Moved Permanently
Locationhttp://www.example.com/subs/blog/

KeyValue
ResponseHTTP/1.1 301 Moved Permanently
Locationhttp://blog.example.com/

lucy24




msg:4589737
 1:23 am on Jul 3, 2013 (gmt 0)

IE does not update the url in the browser if the page is not found

?
If the page isn't found, the URL isn't supposed to change. The 404 should be displayed at the original URL.

The only thing I can complain about now is this url format :

www.blog.test.com

redirects to www.test.com/subs/blog/ then

redirects to blog.test.com

because it has two redirects in one.

You've got the rules in the wrong order. In fact this may be an artifact of having a rewrite before a redirect; is it still happening?

mihomes




msg:4589743
 1:49 am on Jul 3, 2013 (gmt 0)

Lucy, if you look the redirects are BEFORE the rewrite as you advised earlier.

I think you misunderstood my other comments as well - all of this should be happening before a valid page, 404, whatever have you is returned - either way it works fine ie just doesn't update the browser url as other browsers do. You need to look at the headers to see it is working properly.

EDIT - All I'm saying is the other browsers update the change even if there is a 404 while ie just goes straight to the 404. Don't want that misunderstood.

[edited by: mihomes at 2:17 am (utc) on Jul 3, 2013]

lucy24




msg:4589750
 2:13 am on Jul 3, 2013 (gmt 0)

I meant that if you're getting a double redirect, then the rules making the redirect are in the wrong order. Here it's potentially worse, because there should never be a redirect --as opposed to an internal rewrite-- to the /subs/ directory at all.

Are you really saying that MSIE takes you to a page, but doesn't display its correct URL? That seems excessive even for MSIE :(

btw, you pasted in something that has caused this thread's encoding to go haywire. In 8859-1 it comes through as ýý (y-acute, doubled); in UTF-8 there's a blizzard of black diamonds. Understandable, since FD by itself can't occur. Phranque, have you got the scissors?

mihomes




msg:4589755
 2:19 am on Jul 3, 2013 (gmt 0)

I'm aware. The garbage was just from the timing images - there is really nothing to look at other than the url and response code. GET 301 and GET 200 for example.

I should add the double redirect ONLY occurs when using 'www' in front of a subdomain... www.blog.example.com/* in this case.

phranque




msg:4589786
 3:51 am on Jul 3, 2013 (gmt 0)

that still looks precisely like the response you would get with the rules in the initial order.
are you sure you cleared your browser cache and any proxy caches?

EDIT - All I'm saying is the other browsers update the change even if there is a 404 while ie just goes straight to the 404. Don't want that misunderstood.

are you saying the IE browser shows the 404 for the initially requested url and the other browsers show the 404 for the last url in the redirect chain which actually provided the 404 response?

mihomes




msg:4590189
 7:43 am on Jul 4, 2013 (gmt 0)

Well, I got this figured out or at least it is working in all scenarios now. I added another block to handle www in front of the subdomain if any. The result is there will only be one 301 at any given scenario. If there is any optimization that can be done please chime in as I think this thread may be of use to others.

The following redirects are handled :

example.com/subs/<subdomain>/* -> <subdomain>.example.com/*
www.example.com/subs/<subdomain>/* -> <subdomain>.example.com/*
www.<subdomain>.example.com/* -> <subdomain>.example.com/*
example.com/* -> www.example.com/*

The proper internal rewrite is handled :

<subdomain>.example.com/* -> example.com/subs/<subdomain>/*

and here is the code :


RewriteEngine On

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://%1%{REQUEST_URI} [R=301,L]

# BEGIN non-www to www
RewriteCond %{HTTP_HOST} !^(([a-z0-9][-a-z0-9]+)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond %{ENV:REQ_SUBDOMAIN} ^$
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z0-9][-a-z0-9]+)\.example\.com)?$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{DOCUMENT_ROOT}/subs/%2 -d
RewriteRule (.*) /subs/%2/$1 [E=REQ_SUBDOMAIN:%2,L]


As I said if there is any optimization that can be done please chime in. I changed this so it is all dynamic except for the first block where you do not want the rewrite to occur if in one of your subdomains. In this example (blog).

Most all of this was from other sources that I modified slightly for my needs.

mihomes




msg:4590191
 7:56 am on Jul 4, 2013 (gmt 0)

I should also note that in the dns for the domain I am using to test there is an a record for both :

blog and www.blog

I can only assume this is the default as this is how whm handled the creation of the blog subdomain. Perhaps just removing the www.blog would have solved the earlier problem, but then any www.subdomain.example.com requests would be invalid whereas this redirects them properly to subdomain.example.com

lucy24




msg:4590217
 9:34 am on Jul 4, 2013 (gmt 0)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

It seems as if there ought to be a rule for when the host is blog.example.com. The situation may never really occur-- but then, neither should the various /subs/ scenarios. It's just for insurance, right? And if there's a preceding rule for "IS blog.example.com" then you can throw out the "IS NOT" condition in the present rule.

RewriteRule (.*) http://%1%{REQUEST_URI} [R=301,L]

Tiny point here: Either capture and reuse the request-- with parentheses-- OR use {REQUEST_URI} --without parentheses. No point in looking up the same thing twice.

Personally I'd stick with the capture. But then, anything involving %{blahblah} in the target makes me anxious ;)

mihomes




msg:4590796
 12:34 pm on Jul 6, 2013 (gmt 0)

It seems as if there ought to be a rule for when the host is blog.example.com. The situation may never really occur-- but then, neither should the various /subs/ scenarios. It's just for insurance, right? And if there's a preceding rule for "IS blog.example.com" then you can throw out the "IS NOT" condition in the present rule.


My thought process for this was down the road when there are more subdomains. Then I can just do a (blog|sub2|sub3|etc)

You are correct though, there should never be a request for the subs directory, but better to cover all bases so there would never be dupe content.

Tiny point here: Either capture and reuse the request-- with parentheses-- OR use {REQUEST_URI} --without parentheses. No point in looking up the same thing twice.

Personally I'd stick with the capture. But then, anything involving %{blahblah} in the target makes me anxious ;)


Not following you on this one and I will apologize early. Just don't have much experience with these rewrites outside of a domain redirect or www/non-www. Can you elaborate?

mihomes




msg:4590956
 11:25 am on Jul 7, 2013 (gmt 0)

I did some 'studying' and have to admit I have a better grasp of all these now.

for the part mentioned in the last post I changed


# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://%1%{REQUEST_URI} [R=301,L]


to

# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.(.+)\.example\.com$
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]


Now the question is... is that more optimized? Like I said, still getting the hang of these. I was able to remove the check for a non-subdomain and from what I understand am not doing a double lookup either.

lucy24




msg:4591022
 8:58 pm on Jul 7, 2013 (gmt 0)

RewriteCond %{HTTP_HOST} ^www\.(.+)\.example\.com$

The .+ in the middle makes no sense. What is needed is an explicit
\w+
or, if you're on one of those hypothetical Apache installations that pretend not to recognize the form,
[a-z]+ with [NC] flag
Or conceivably [a-z-] if you've got weird subdomain names. (Is that even legal?)

And why is everything suddenly in an ugly font?

Now, if it happens in actual practice that none of your subdomain names begins in "e"-- where "e" means, ahem, the real first letter of your real domain name-- then you might be able to get out of there even faster by expressing it as

www\.([^e][a-z]+)\.example\.com

replacing + with * if you've got one-letter subdomain names. (Not likely!)

mihomes




msg:4592671
 5:11 am on Jul 13, 2013 (gmt 0)

Back with another issue. I will be adding a wildcard ssl cert and have added in redirects. At this time only the 'secure' subdomain will be https and everything else will be http. I have added two redirects to force this and appears to work fine in my tests.

What I am now having trouble with is redirecting index.(php|html?) to the directory it is in preserving the parameters if any and excluding one directory. Essentially ensuring short links without the index filename. The rules work fine for the main domain, but cause an infinite loop for subdomains.

Examples that work :

www.example.com/index.html?p=test -> www.example.com/?p=test

www.example.com/test/index.html?p=test -> www.example.com/test/?p=test

Example that does not work :

blog.example.com/index.html

This causes an infinite loop as it redirects to 'http://blog.example.com/sub_ds/blog/' over and over. Why is it referring to the 'real' location of the file rather than the redirected 'new' location as per the rules above it? Apparently that is now how they work :(

Options +SymLinksIfOwnerMatch

# BEGIN rewrites
RewriteEngine On

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sub_ds/
RewriteRule ^sub_ds/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.([a-z0-9-]+)\.example\.com
RewriteRule ^sub_ds/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

# Externally redirect 'secure' subdomain to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Externally redirect non 'secure' subdomain to http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^secure\.
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Externally redirect index.(php|html?) in any location, preserving parameters, to location root
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /phpsite/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(php|html?)
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://%{HTTP_HOST}/$1 [R=301,L]


# BEGIN non-www to www
RewriteCond %{HTTP_HOST} !^(([a-z0-9][-a-z0-9]+)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/sub_ds/<subdomain/<URLpath>
RewriteCond $1 !^sub_ds/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sub_ds/%1/$1 [L]

# BEGIN expires rules
ExpiresActive On
ExpiresByType text/html "access"

[edited by: phranque at 7:54 am (utc) on Jul 13, 2013]
[edit reason] exemplified hostname [/edit]

lucy24




msg:4592716
 10:12 am on Jul 13, 2013 (gmt 0)

What I am now having trouble with is redirecting index.(php|html?) to the directory it is in preserving the parameters if any and excluding one directory. Essentially ensuring short links without the index filename.

<tangent>
The idea behind an index-redirect is primarily to avoid duplicate content. If you want a shorter URL that's a whole nother issue.
</tangent>

Since parameters are preserved untouched unless you specifically say otherwise, they should have no effect on redirects and can simply be ignored.

This causes an infinite loop as it redirects to 'http://blog.example.com/sub_ds/blog/' over and over. Why is it referring to the 'real' location of the file rather than the redirected 'new' location as per the rules above it?

You've got a rewrite in the mix somewhere. Rewrites use "real" paths. That's why it is so important to ensure that nothing gets redirected after it has already been rewritten.

Have you quoted your entire htaccess? That is, not literally the whole thing-- we don't need to see your IP lockouts and similar :)-- but absolutely everything involving rewrites and/or redirects? And there's nothing else in any deeper htaccess?

phranque




msg:4592739
 1:41 pm on Jul 13, 2013 (gmt 0)

# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>

for the purpose of clarity, this comment is incorrect and to match the ruleset should be:
# Externally redirect client requests for example.com/sub_ds/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>

# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>

for the purpose of clarity, this comment is incorrect and to match the ruleset should be:
# Externally redirect client requests for www.[subdomain].example.com/sub_ds/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>

however in this case i'm guessing your original comment is correct and you really want this ruleset:
# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.([a-z0-9-]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]




RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://%{HTTP_HOST}/$1 [R=301,L]

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
Pattern is a perl compatible regular expression. On the first RewriteRule, it is matched against the (%-decoded) URL-path (or file-path, depending on the context) of the request.

if i had to guess, you have a DirectoryIndex directive that specifies index.php and the rule above gets fired in file-path context when the request is for sub_ds/blog/index.php, relative to the document root directory.
this means your internal file path gets exposed as an external url and then your first ruleset fires on the subsequent client request.
you probably want this:
# Externally redirect index.(php|html?) in any requested location, preserving parameters, to location root
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /phpsite/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]


i could be off base here...
if you have access to server config it might be worth doing some rewrite logging to figure it out.

This 47 message thread spans 2 pages: 47 ( [1] 2 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved