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

Apache Web Server Forum

This 31 message thread spans 2 pages: 31 ( [1] 2 > >     
redirecting subdomain to subfolder
surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 10:42 am on Sep 26, 2012 (gmt 0)

I am updating a site of mine and would like to redirect a fair few subdomain URL's to subfolders.

Here's an example of whats currently setup.

galleries.example.com/name-of-gallery/randomnumbers
galleries.example.com/another-gallery-name/randomnumbers

I would like to forward them to

example.com/gallery/name-of-gallery
example.com/gallery/another-gallery-name

The folders are going to be setup with the correct names. The problem is, how to do this via .htaccess redirects without writing each one out individually.

The random numbers at the end of the subdomain URL's are probably going to cause a pain but there's nothing I can do about them.

I could write them all out, but that would make for a very large .htaccess file - unless that's the only way to do this?

 

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 2:21 pm on Sep 26, 2012 (gmt 0)

I've had a bit of looking around at Regex and mod_rewrite and have come up with this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^galleries.example.com/([a-z0-9]+)/([0-9]+) [NC]
RewriteRule ^(.*)$ http://www.example.com/gallery/%1 [L,NC,QSA]

Would this be along the lines of what I'm after? Or would it cause the destruction of the universe because I have no apparent idea what I just did?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 7:07 pm on Sep 26, 2012 (gmt 0)

Or would it cause the destruction of the universe because I have no apparent idea what I just did?

It is always good to ask this question before rather than after ;) If you had waited until after, your question would have been
I tried
{code as above}
but it doesn't seem to work

and then it would take a big runaround getting you to explain what you mean by "doesn't work". In this case, the answer is: the rule would never execute.

HTTP_HOST refers only to the domain. So, in your case, only the "galleries.example.com" part. The rest of the URL goes in the body of the Rule.

Do you want a rewrite or a redirect? (In English: Do you want the user's address bar to change?) As written, you would get a 302 temporary redirect because there is no R=301 flag but the target includes the full protocol-plus-domain package.

Where do the subdomains currently live? It's normal for them to physically live in subdirectories within your top-level directory, but the URL doesn't have to say so. If they already exist, I assume your host is dealing with that aspect. Are you trying to get rid of the subdomain? If so, a redirect really is what you need, but you have to include a [R=301] flag to make it a "real" permanent redirect.

Do your current URLs actually have queries? If not, the [QSA] flag isn't needed. In fact it wouldn't be needed anyway, since your target doesn't say anything about a query. So it's reappended by default.

Finally there's no need for [NC] in the Rule, even after you change it to put the body of the URL in the "pattern" side of the Rule rathern than as a condition. Say \w or [A-Za-z0-9] instead.

Oh, and bonus points in advance because your first try contained no trace of non-final .* anywhere :)

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 9:23 pm on Sep 26, 2012 (gmt 0)

Well...I did a bit more playing around after the post.

But in response to a few of your points.
I'd want a redirect, users should be sent to the new address, and the address bar should reflect this. I realise there is no R=301 at present, but there should be one on the finished article.

A bit of reading up on Regex's in mod_rewrite, and I've learnt what the QSA is, and have got rid, likewise with the NC.

And I like bonus points ;) Even though I'm not sure why there's no trace of .* and why I haven't included it.

Back to some testing I did.
I've narrowed it to this bit of code, which thankfully (in case you haven't noticed) hasn't caused the destruction of the universe:

RewriteRule ^galleries.example.com/([a-zA-Z0-9-]+)/([0-9]+)/?$ http://www.example.com/gallery/%1/$1 [L]

The original subdomain is currently pointed away from the normal server (another DNS), but will be changed back. I've done some testing on another subdomain (same DNS) to see if it works or not...and it doesn't.

According to online htaccess testers, there doesn't seem to be anything wrong with the code, but yet a redirect isn't happening...so I'm a bit stuck as to what to do...

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 9:27 pm on Sep 26, 2012 (gmt 0)

The pattern for the rule can match only the path part of the URL, not hostname nor query string.

Where does the value for %1 come from?

Without a capture in a RewriteCond, it will always be blank.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 10:13 pm on Sep 26, 2012 (gmt 0)

Uh...right? That's makes me more confused.

The %1 grabs the first variable from the Regex, or so I believe. In that case, ([a-zA-Z0-9-]+) is the first variable, unless its not written correctly.

Was the first code I wrote closer to what I need than the 2nd bit of code?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 11:48 pm on Sep 26, 2012 (gmt 0)

%1 comes from the first capture in an immediately preceding RewriteCond.

$1 comes from the first capture in the RewriteRule pattern.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 12:39 am on Sep 27, 2012 (gmt 0)

The original subdomain is currently pointed away from the normal server (another DNS), but will be changed back. I've done some testing on another subdomain (same DNS) to see if it works or not...and it doesn't.

Oh, ###, that makes all the difference. The htaccess has to be located where the original request will see it. That means in the subdomain's current home, wherever it may be.

Since you're capturing two things from the Rule, it would be www.example.com/$1/$2 etc. But it could perfectly well also be

([a-zA-Z0-9-]+/[0-9]+)

as a single $1 capture, since you're using both pieces in their original order.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 10:09 am on Sep 27, 2012 (gmt 0)


Oh, ###, that makes all the difference. The htaccess has to be located where the original request will see it. That means in the subdomain's current home, wherever it may be.


Ah, I've been placing it inside the root .htaccess of the domain, not the subdomain.

However, still no luck. Placed it inside a testing subdomain, but I just get a 404.


Since you're capturing two things from the Rule, it would be www.example.com/$1/$2 etc. But it could perfectly well also be

([a-zA-Z0-9-]+/[0-9]+)

as a single $1 capture, since you're using both pieces in their original order.


Although there's a random bunch of numbers after the last /, I'm not actually using them in the final destination. I just need the ([a-zA-Z0-9) part to forward to the new destination of the same name.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 6:28 pm on Sep 27, 2012 (gmt 0)

Bracket the bit you want to re-use, and if it's the first bracket then you can find it in $1 in the target URL.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 7:46 pm on Sep 27, 2012 (gmt 0)

The original subdomain is currently pointed away from the normal server

I've been placing it inside the root .htaccess of the domain, not the subdomain.

If I'm reading this right, requests for the subdomain currently get sent to a completely different server. That means they will never see directives in the main domain's server-- regardless of directory-- because they don't get there until after they've been redirected.

Who's doing the pointing? Is it at the DNS level, or something that the host has cobbled together?

Elsewhere: If you're throwing away the last part of the URL, you don't need to capture it-- or even look at it, unless you need to make sure it does exist.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 8:49 pm on Sep 27, 2012 (gmt 0)

Still not getting anywhere.

RewriteRule ^testing.example.co.uk/([a-zA-Z0-9-]+)/$ http://www.example.co.uk/gallery/$1/ [L,NC]

Still getting 404's.

I have setup a testing subdomain to test this on. All DNS records for both this subdomain and root domain are the same.

For the actual one I'll be putting this on..the DNS records will be changed back so the entire domain (subs and root) will also be the same DNS. That's not the issue here.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 5:29 am on Sep 28, 2012 (gmt 0)

Waitwaitwait. Where did that RewriteRule come from? Isn't there an entirely different and unrelated thread about someone who goofed bigtime and duplicated his domain name in the body of his URLs?

I'm so confused.

:: sob ::

It's no use testing anything if the environments aren't equivalent. Is your dummy subdomain located in a subdirectory inside the real domain? Then either the htaccess has to go within that subdirectory, where only requests for the subdomain will see it, or* the Rule has to to have an accompanying RewriteCond that says

... %{HTTP_HOST} subdomain.example.co.uk

and then the Rule itself kicks in at the ([a-zA-Z0-9-]+) stage.


* Either/or: Doing both things will not break the server, but it really is superfluous.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 7:00 am on Sep 28, 2012 (gmt 0)

The pattern for the rule can match only the path part of the requested URL, not hostname nor query string.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 10:13 am on Sep 28, 2012 (gmt 0)


Is your dummy subdomain located in a subdirectory inside the real domain?


Yes. The .htaccess with only this rewrite rule sits inside that subdirectory.

I still don't get why its not working though. Should that rule work the way I've got it or not? What do I need to write to make it work?


The pattern for the rule can match only the path part of the requested URL, not hostname nor query string.


I dont understand what you mean by this?

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4500118 posted 10:58 am on Sep 28, 2012 (gmt 0)

The pattern for the rule can match only the path part of the requested URL, not hostname nor query string.

I dont understand what you mean by this?

this:
RewriteRule ^testing.example.co.uk


if you need to test or capture hostname information you'll have to do that in a preceding RewriteCond in that ruleset.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 11:47 am on Sep 28, 2012 (gmt 0)

Ok. Think I get that now..but I still dont get why this doesn't work.

I've reverted back to this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^testing.example.co.uk/([a-zA-Z0-9-]+)/[0-9]
RewriteRule ^(.*)$ http://www.example.co.uk/gallery/%1/$1 [L,NC]

The regex is correct, the domains are correct, subdomains are on the right DNS, htaccess file is in the right place. But all I get is a 404 error.

Online htaccess testers says the ReWriteCond was not met - but testing out the url with the regex in a regex tester and it was all correct.

The url is

testing.example.co.uk/Name-of-Gallery-2012/2475685658

I just want it to work, is that not difficult?!? My heads going to explode...

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4500118 posted 12:22 pm on Sep 28, 2012 (gmt 0)

HTTP_HOST is only going to contain hostname information, so that's all you can test with that RewriteCond directive.
if you want to capture path information you can put that pattern in the RewriteRule.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 12:31 pm on Sep 28, 2012 (gmt 0)

I still dont understand it. Could you use some examples please?

I thought I am capturing path information? To me, those lines all should work the way I want them to...but they're obviously not and that's confusing me.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 4:25 pm on Sep 28, 2012 (gmt 0)

The RewriteRule pattern matches only the PATH part of the requested URL.

A RewriteCond testing HTTP_HOST matches only the requested hostname.

A RewriteCond testing QUERY_STRING matches only the requested query string.

Be sure to escape all literal periods in patterns.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 8:39 pm on Sep 28, 2012 (gmt 0)


The RewriteRule pattern matches only the PATH part of the requested URL.
A RewriteCond testing HTTP_HOST matches only the requested hostname.
A RewriteCond testing QUERY_STRING matches only the requested query string.


The rules I wrote all match up for me though. Hence why I said, to me, what I wrote should work..but it doesn't.


Be sure to escape all literal periods in patterns.


There are no literal periods in the pattern. The only thing I wasn't sure about was the - but since that is at the end of the regex, and not between 2 characters, then the server should know its a - and not used in a range.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 10:36 pm on Sep 28, 2012 (gmt 0)

Your HTTP_HOST test is trying to match hostname and path and always fails.
A RewriteCond testing HTTP_HOST matches only the requested hostname.

There are three literal periods in
testing.example.co.uk
It should be written
testing\.example\.co\.uk within any RegEx pattern.
lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 2:49 am on Sep 29, 2012 (gmt 0)

RewriteCond %{HTTP_HOST} ^testing.example.co.uk/([a-zA-Z0-9-]+)/[0-9]
RewriteRule ^(.*)$ http://www.example.co.uk/gallery/%1/$1 [L,NC]


HTTP_HOST = testing.example.co.uk AND THAT'S ALL. If you put anything more in this line, the Condition will fail.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 9:27 pm on Sep 29, 2012 (gmt 0)

Ah ok, thanks.

However..if I remove the {HTTP_HOST} then I get a 500 error.

But, if I remove the testing.example.co.uk and leave in the {HTTP_HOST}, I get a 404.

So..which bits/characters do I need to delete, and which bits/characters do I need to keep?

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 9:41 pm on Sep 29, 2012 (gmt 0)

I'm going round in endless circles here, and its giving me a headache. I just need this to work. No cryptic messages expecting me to understand it all. If I did understand how all this worked, then I wouldn't be asking for help in the first place.

This is going on for ages and I just need this to work. Is that not too difficult to ask for please?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4500118 posted 10:44 pm on Sep 29, 2012 (gmt 0)

Why are you deleting {HTTP_HOST} or testing\.example\.co\.uk ?

Those are the bits you should be keeping.


You need to get familiar with the terminology so that you can maintain your code over time.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 12:45 am on Sep 30, 2012 (gmt 0)

I'm going round in endless circles here

Took the words right out of my mouth. In fact you almost seem to be going backward.

RewriteCond %{HTTP_HOST} ^testing\.example\.co\.uk

AND THAT'S ALL

The opening anchor is not technically necessary, because I assume you won't have other subdomains named, say, "attesting.example.co.uk", but it does no harm. Do leave off the closing anchor in case someone gets smart and sticks a port number onto the end of the hostname.

In this specific situation, forgetting to escape the literal periods (. vs. /.) will have no effect, because requests for "testingaexamplebcocuk" will never reach your server. But do it as a matter of habit anyway.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 8:50 pm on Sep 30, 2012 (gmt 0)


RewriteCond %{HTTP_HOST} ^testing\.example\.co\.uk

AND THAT'S ALL


Then...how do I capture the part of the URL I need for the redirect?


Why are you deleting {HTTP_HOST} or testing\.example\.co\.uk


Because I don't know what I'm doing ;)
I thought they were the same thing, so, one wasn't needed?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500118 posted 10:13 pm on Sep 30, 2012 (gmt 0)

how do I capture the part of the URL I need for the redirect?

By putting it in the body of the Rule. Never put something in a Condition that can go in the Rule itself. Saves work for the server. In this case, you would have to have two separate Conditions to achieve the same result-- and you can only capture from the last one.

To grab

subdomain.example.co.uk/filename.html
subdomain.example.co.uk/otherfile.html

and send them off to your real domain, the Rule-and-Condition package would generally look something like

RewriteCond %{HTTP_HOST} ^subdomain\.example\.co\.uk
RewriteRule ^(filename|otherfile)\.html http://www.example.com/$1.html [R=301,L]

I thought they were the same thing, so, one wasn't needed?

Spaces in mod_rewrite have syntactic meaning. Here,
RewriteCond %{HTTP_HOST} ^subdomain\.example\.co\.uk
means

IF
the host (the part on the left)
begins with "subdomain\.example\.co\.uk" (the part on the right)
THEN
go on to the next Condition-- or execute the Rule if you've got that far.

In the body of the rule, you've got

IF
the request is for (stuff that fits the part on the left)
THEN
evaluate the Conditions, and if all Conditions are met-- or if there aren't any Conditions--
THEN
point the request to (part on the right)
... with further instructions provided by flags, if any.

surrealillusions

5+ Year Member



 
Msg#: 4500118 posted 10:31 pm on Sep 30, 2012 (gmt 0)

Ah right, thanks...the redirect is now working. All seems to be ok so far.

Thanks for your patience :)

(Till I somehow break it...)

This 31 message thread spans 2 pages: 31 ( [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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved