homepage Welcome to WebmasterWorld Guest from 54.243.13.30
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess rewrite subdomains partly working
Eefje123




msg:4590011
 7:33 pm on Jul 3, 2013 (gmt 0)

Hi,

Hope someone can help me. I had some trouble with duplicate url's, so i needed to change my .htaccess file. After a lot of trying and a lot of errors I found something that seems to work almost.

I have a domain and several subdomains that I redirect like this:

RewriteCond %{http_HOST} !^subdomain1\.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/(subdomain1)/ [NC]
RewriteRule (.*) http://subdomain1.example.com/$1 [R=301,L]

RewriteCond %{http_HOST} !^subdomain2\.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/(subdomain2)/ [NC]
RewriteRule (.*) http://subdomain2.example.com/$1 [R=301,L]

and so on

This works the way I want it. If you ask for
http://www.example.com/subdomain/index.php the url changes to
http://subdomain.example.com/subdomain/index.php

So that works ok. Except for this:

If you ask for
http://subdomain.example.com without anything, the url stays the same, but you see http://www.example.com

I have no idea how to fix this.

[edited by: Ocean10000 at 8:41 pm (utc) on Jul 3, 2013]
[edit reason] Fixed AutoLinking [/edit]

 

phranque




msg:4590085
 10:46 pm on Jul 3, 2013 (gmt 0)

welcome to WebmasterWorld, Eefje123!


and so on

how many of these rulesets are there?

If you ask for
http://subdomain.example.com without anything, the url stays the same, but you see http://www.example.com

that looks like it's working as you configured it, assuming you have a general domain canonicalization redirect to www.example.com at the end.
you should test for valid subdomains before the www.example.com redirect.

http://subdomain.example.com/subdomain/index.php

i would specify a default directory index document and redirect any requests for index.php urls to the trailing slash url.
why do you want "subdomain" in both the hostname and the path?

lucy24




msg:4590094
 11:05 pm on Jul 3, 2013 (gmt 0)

RewriteCond %{REQUEST_URI} ^/(subdomain1)/ [NC]
RewriteRule (.*) http://subdomain1.example.com/$1 [R=301,L]

This type of rule-and-condition pair can always be expressed as

RewriteRule ^(subdomain1/.*) http://subdomain1.example.com/$1 [R=301,L]

Never put something in a condition that can go in the body of the rule. This applies especially to the {REQUEST_URI} element. Here, it means that mod_rewrite doesn't even need to look at the Conditions if the RequestUri doesn't match.

Ditto to phranque's question of why the subdomain is named twice. What I'd expect to see is

RewriteRule ^subdomain1/(.*) http://subdomain1.example.com/$1 [R=301,L]

...and at this point you don't even need a Condition looking at the host, because you'll be redirecting either way.

Eefje123




msg:4590360
 7:00 pm on Jul 4, 2013 (gmt 0)

Thank you both.

@Phranque

I have 18 of these rulesets.
And this before the rules:

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

That's all.

I use the subdomain twice because in some cases the name of the subdomain is different from the name of the folder.


@ Lucy

I tried your codes

RewriteRule ^(subdomain1/.*) http://subdomain1.example.com/$1 [R=301,L]

Works the same as the code I am using

RewriteRule ^subdomain1/(.*) http://subdomain1.example.com/$1 [R=301,L]

If you ask www.example.com/subdomain/anyfile.php

It gives: www.example.com/anyfile.php and that does not exist.

[edited by: phranque at 7:17 pm (utc) on Jul 4, 2013]
[edit reason] unlinked urls [/edit]

phranque




msg:4590367
 7:30 pm on Jul 4, 2013 (gmt 0)

If you ask for http://subdomain.example.com without anything, the url stays the same, but you see http://www.example.com


i might have misunderstood you here.
does "the url stays the same" mean there is no 301 redirect and the address bar still shows this?
http://subdomain.example.com
and "you see http://www.example.com" means you see the content you expected to see for the www.example.com home page instead of the subdomain.example.com home page?

in this case perhaps there is an internal rewrite you aren't showing us.


another possibility is that http://subdomain.example.com is a non-canonical url and needs to be redirected to http://subdomain.example.com/subdomain/
or stated another way - should you ever serve content (200 OK) when the request is for the root directory of the subdomain?

lucy24




msg:4590389
 9:36 pm on Jul 4, 2013 (gmt 0)

RewriteRule ^(subdomain1/.*) http://subdomain1.example.com/$1 [R=301,L]

Works the same as the code I am using

Well, it is the code you am using. The question was why the element "subdomain" appears twice in the URL.

I use the subdomain twice because in some cases the name of the subdomain is different from the name of the folder.

Now, wait. Do you mean "folder" as in "element of URL" or "folder" as in "physical directory"?

If you have eighteen rulesets involving subdirectory redirects, then you must also have eighteen rulesets-- or a smaller group using pipes and captures-- involving rewrites from the URL to the physical location of the content. In a three-dimensional universe, forms like
subdomain.example.com/
cannot directly represent a physical location in the way that
example.com/subdomain/
can.

Eefje123




msg:4590467
 9:31 am on Jul 5, 2013 (gmt 0)

Yes like you say: the url bar says subdomain.example.com and does not change, but is showing the index page of www.example.com


Ok, I see.

You mean the panel of the provider where I can make subdomains?

When I don't use .htaccess at all:

subdomain.example.com also shows the index page of www.example.com

and

www.example.com/subdomain /somepage
and
domain.example.com/subdomain/somepage
are both working and point to the same page and that is exactly the problem why I tried using .htaccess.

the provider gave me this code

RewriteCond %{HTTP:Host} ^((?!www\.)(.+)|(?:www\.)(.+))\.(example\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? /%2%3%5 [L]

this works for the subdomains that have the same name as the folder (I said folder but as Lucy said, i meant physical directory) for the subdomain.example.com to show the right page. But all the other pages are not found unless you use:

subdomain.example.com/somepage

This would not have been a problem if I had used this from the beginning but now I'll loose all external en internal links because I used full url's.

lucy24




msg:4590632
 8:34 pm on Jul 5, 2013 (gmt 0)

You won't lose external links entirely. You'll have to redirect them, but it isn't a huge loss if it can be done in one step.

Internal links-- assuming you mean from within the same overall domain-- should never have been an issue in the first place. Put the links in their desired final form.

RewriteCond %{HTTP:Host} ^((?!www\.)(.+)|(?:www\.)(.+))\.(example\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? /%2%3%5 [L]

What the bleep? For years I've had it hammered into my head that % elements only refer to the last matched condition, here meaning the second of two.

:: detour to docs ::

Yup, right there, no change between 2.2 and 2.4. So what does "last matched" mean if it doesn't mean "last matched"?

Setting aside the whole issue of unspeakable insanity of using numbered captures within a pipe-delimited set of alternatives

For subdomains whose directory name is different from the subdomain name, you'll have to write explicit case-by-case rules. In particular you have to make sure these rewrites are excluded from the generic rewrite, or else you'd be rewriting to subdomains that don't exist.

I have no idea why the host's recommended RewriteRule puts the RequestUri into a separate RewriteCond instead of simply capturing from the body of the rule. This would also get rid of the "last matched" head-scratcher because you're left with only one condition.

phranque




msg:4590664
 10:47 pm on Jul 5, 2013 (gmt 0)

it seems like all of your hostname are using the same document root directory as www.example.com.

i would suggest what you really want may be separate virtual host configurations for each subdomain that each use that subdomain's directory as their document root directory and then get rid of the subdomain directory in the url path.

lucy24




msg:4590714
 1:28 am on Jul 6, 2013 (gmt 0)

The first post mentioned htaccess, so virtual hosting may not be on :(

phranque




msg:4590718
 1:35 am on Jul 6, 2013 (gmt 0)

in that case, each virtual host configuration could be on its own server.

Eefje123




msg:4590817
 2:09 pm on Jul 6, 2013 (gmt 0)

Like you probably noticed, the code is all like Chinese to me. And I know nothing about virtual servers.

But, I found an easy way out:

I'll leave the htaccess file the way it is (with Lucy's changes) and In my index.php from example.com I added for each subdomain:

$SiteNameURL = $_SERVER['HTTP_HOST'];
switch (strtolower($SiteNameURL)) {
case "subdomain.example.com": //MUST BE LOWER CASE
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/subdomain/index.php");
break;


Now subdomain.example.com changes to subdomain.example.com/subdomain/index.php

Maybe not the best solution, but it works.
So thank you both for your input and time.

Eveline

lucy24




msg:4590878
 8:09 pm on Jul 6, 2013 (gmt 0)

but it works.

Where "works" = "this code comes before the page has output any text of any kind, so the header is successfully sent" ?

Go for it.

phranque




msg:4590908
 11:09 pm on Jul 6, 2013 (gmt 0)

I found an easy way out

not so fast, Eveline.

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/subdomain/index.php");

get rid of the index.php and don't refer to or redirect to any internal urls that use index.php.

instead, use this in your .htaccess file:
DirectoryIndex index.php

then you should redirect any requests for index.php urls to the trailing slash url.


Maybe not the best solution, but it works.

i would test to insure that you don't get a chain of redirects for some requests.
all non-canonical requests should be redirected to the canonical url in one hop.

Eefje123




msg:4591153
 12:48 pm on Jul 8, 2013 (gmt 0)


Ok, i made the changes. I did some tests and so far everything seems to work fine.

Thanks again.

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