Forum Moderators: phranque

Message Too Old, No Replies

Re-writing directory to subdomain

         

ivanvias

1:04 am on Jul 7, 2014 (gmt 0)

10+ Year Member



Hi I would like to rewrite my old urls like these :


[example.com...] to go to firstnamelastname.example.com

RewriteCond $1 !^(support|about-us|memorial-website-specials|profile|support|features|videos|newserver|terms-of-service|about-us|funeralnotice|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ https://$1.example.com/$2 [R=301,L]

lucy24

6:34 am on Jul 7, 2014 (gmt 0)

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



... and?

Your lips say "rewrite rewrite rewrite" but your eyes say "redirect redirect redirect".

How would the condition looking at THE_REQUEST exclude items that have already been redirected? Seems like you'd instead want the first condition to look at HTTP_HOST.

Psst!
[^\ ] = \S
This is an especially useful detail in Apache, where literal spaces have to be escaped, and you don't have things like tabs or line breaks to worry about.

Do your "firstnamelastname" old URLs ever contain hyphens? If no, you can simplify a lot. But that's no help if your members are all named Jean-Luc and Ti-Grace. But surely they don't have numerals in their names? That's assuming when you say "firstnamelastname" you really are talking about human names who are being promoted to subdomain status.

ivanvias

6:21 pm on Jul 7, 2014 (gmt 0)

10+ Year Member



Yeah those rules don't work on my site. If I type example.com/firstnamelastname it doesn't revert to firstnamelastname.example.com Names only contain letters. Not sure why this is not working.

lucy24

8:56 pm on Jul 7, 2014 (gmt 0)

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



it doesn't revert to

Do you mean that the rule has no effect at all? Ask for
www.example.com/firstnamelastname/blahblah

and that's what the browser's address bar ends up saying, instead of the intended
firstnamelastname.example.com/blahblah

?

If the redirect appears to take place-- address bar changes-- but you end up with an error message, then there are other, unrelated problems. Are you set up for wild-card subdomains? Do you have the appropriate rewrite (not redirect) somewhere else, so the server can find the subdomain content?

Do other RewriteRules work? That's always the first thing to check; it's equivalent to "Is the device plugged in?"

If nobody has a hyphenated name, you can express the pattern as
^([A-Za-z]+)/

and then in the RewriteCond you can leave out any directory whose name contains hyphens, numerals or other non-alphabetics, because they won't match in the first place. (Rules are evaluated before conditions. It's backward.)

Incidentally, what about casing of names in the URL? Is it FirstnameLastname or firstnamelastname?

ivanvias

2:05 am on Jul 8, 2014 (gmt 0)

10+ Year Member



The casing of names is firstnamelastname. The rules dont seem to resolve to anything.

I am basically trying to achieve or rewrite all urls that are like so: [example.com...] to [firstnamelastname.example.com...]

ivanvias

2:06 am on Jul 8, 2014 (gmt 0)

10+ Year Member



All i would get is a 404 not any other errors.

ivanvias

9:26 pm on Jul 10, 2014 (gmt 0)

10+ Year Member



Can anyone assist me in getting this rule done for htaccess. Anyone here?

I want to be able to rewrite [example.com...] to [firstnamelastname.example.com....]

lucy24

12:54 am on Jul 11, 2014 (gmt 0)

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



Sorry, ivan, missed the follow-up posts.

I am basically trying to ... rewrite all urls that are like so: [example.com...] to [firstnamelastname.example.com<...]
Please stop saying "rewrite". Your goal is a redirect. Doesn't matter whether it's achieved by mod_rewrite, mod_alias or some other means; all that matters is the server sends out a 301 response.

It's not clear to me whether your redirects are taking place. When you request (type into your browser's address bar)
example.com/johnsmith/more-stuff-here
does the address bar end up saying
johnsmith.example.com/more-stuff-here
or
example.com/johnsmith/more-stuff-here
or something else entirely?

If the redirect is not taking place: Do you have other RewriteRules that work as intended?

:: wandering off to compose boilerplate on Things To Check Before Calling The Repairman ::

ivanvias

12:07 am on Jul 15, 2014 (gmt 0)

10+ Year Member



the address bar only endsup saying example.com/johnsmith/more-stuff-here and same for example.com/johnsmith


I am using these rules here:

#################BEGIN RULES ON LIVE SITE
# Externally redirect direct client requests for subdomain-subdirectory URLs which
# resolve to existing subdirectories back to canonical subdomain root URLs
RewriteCond $1 !^(terms-of-service|about-us|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ [$1.example.com...] [R=301,L]


#
# Externally redirect requests for /features, /about-us, /signup, /images in subdomains to main domain
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(support|about-us|terms-of-service|signup|component|includes|media|cgi-bin|templates|xmlrpc|language|modules|libraries|plugins|administrator|images(/.*))$ [example.com...] [R=301,L]
#
# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ [%1%3example.com...] [R=301,L]
#

lucy24

1:18 am on Jul 15, 2014 (gmt 0)

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



RewriteCond $1 !^(terms-of-service|about-us|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/

Two things.
#1 Didn't you say earlier that your customers (the people with the subdomains) never have hyphenated names? And that everything is lower-case? So you can omit the directories like "about-us", and also constrain the pattern in the body of the rule to
^[a-z]+/etcetera

If numerals never occur anywhere, there's no need even to look for them.

#2 If you've got a choice, choose literal text above headers-or-equivalent, and both ahead of captures. Less work for the server. Here there's no reason for $1 because {REQUEST_URI} will do just as well. And since you're also looking at {THE_REQUEST} it should be possible to combine both in a single condition.

And speaking of {THE_REQUEST}...
RewriteCond %{THE_REQUEST} ^[A-Z]+ /([a-z0-9\-]+)/[^\ ]*\ HTTP/

Again, the first directory-- the names that you're redirecting from-- is only [a-z] right? And the intended pattern is only meant to go one directory deep, so
example.com/johnsmith/more-stuff-here
but not
example.com/johnsmith/directory/more-stuff-here
?
If so, put that into the body of the rule. I may have missed something; I don't see why you have to look at {THE_REQUEST} at all. This condition is only necessary when the URL in question could also occur as the result of an internal rewrite, and you can't cover yourself with a simple [NS] flag.


RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(support|about-us|terms-of-service|signup|component|includes|media|cgi-bin|templates|xmlrpc|language|modules|libraries|plugins|administrator|images(/.*))$ [example.com...] [R=301,L]

Do you have a lot of members named U Nu and A Se? The subdomain names will always be longer than 3 letters, so put that in a single condition:
RewriteCond %{HTTP_HOST} ^[a-z]{4,}\.example\.com


Also, there's a potentially fatal mistake in positioning of parentheses, unless you really do mean that /images/ is the only directory that can contain subdirectories. I strongly suspect you meant
RewriteRule ^((foo|bar|here|there|more|stuff)(/.*)?)$ etcetera

where the form
(/.*)?
means "There may be a final directory slash, and there may be additional stuff after it". Search engines will ask for directory names without final slash, so make sure the rule covers the possibility. Since these are real, physical directories, mod_dir will take care of the missing slash later, as long as mod_rewrite has done what you want. It's possible to write rules so there's no chance of a double redirect (one here, a second one from mod_dir) but really I don't think it's worth it when all you're doing is protecting against other people's mistakes.


# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ https://%1%3example.com/$1 [R=301,L]

Again! Just [a-z] ... and wouldn't that part come after "www."? (Is "subdomain.www.example.com" even possible?) And what are the nested parentheses for? Is there any real possibility of
sub1.sub2.www.sub3.sub4.example.com

? At this point it just seems as if you're looking for trouble ;) but let's get someone who speaks Apache to consider all possibilities.