Forum Moderators: phranque
In a nutshell I need to forward sub.example.com to example.com/xyz/sub.
After reading a lot of posts/guides Ive came up with following :
RewriteCond %{HTTP_HOST} !^www\.?example\.com [NC]
RewriteRule ^(.*)$ %{HTTP_HOST} $1 [C]
RewriteRule ^([^.]+)\.example\.com(.*) /var/www/example.com/xyz/$1$2
With this code I have problems when accessing site via example.com ( 400 - bad request error ) and www.sub.example.com ( same error ) .
I would like example.com / www.example.com to be showing main page, and sub.example.com / www.sub.example.com to be showing /var/www/example.com/xyz/sub ... Is there anyone who could help me in fixing code I was able to come up with? After few hours of trying I gave up ;).
As far as solving example.com accessing problem - I was able to fix it by adding additional rule to match ( RewriteCond %{HTTP_HOST} !^example\.com [NC] ) but I am wondering if its the only way of achieving this ( I mean do I have to use another rewritecond ) . As far as www.sub is concerned, I tried adding [^www]? and many others with no luck.
Thank you very much for your time.
Regards,
Vermon
[edited by: jdMorgan at 12:47 am (utc) on May 25, 2007]
[edit reason] example.com [/edit]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /var/www/example.com/xyz/%1/$1 [L]
Jim
One more question :
When capturing subdomain with :
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
Is there any way to parse the www. at the beggining of the url with subdomain? so www.sub.example.com would be treated as sub.example.com? In this capturing I have tried [www\.]- , ([www\.]-) with .... bad and worse results respectivly ;).
Thank you for your time.
Regards,
Vermon
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^[b](www\.)?[/b]([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /var/www/example.com/xyz[b]/%2/[/b]$1 [L]
This should be done before the rewrite above, and eliminates the need for the fix-up you asked for.
Jim
Thank you for your suggestion. I was able to come up with following :
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) [%1.example.com$1...] [R=301,L]
Which is taking care of redirecting example.com -> www.example.com and www.sub.example.com -> sub.example.com. I dont know if redirecting index.php/html to basic site is also vital?
I have been trying with
RewriteCond %{THE_REQUEST} ^\([^/]+/\)index\.(php多tml)
RewriteRule ^([^/]+/)index\.(php多tml)$ [$1...] [R=301,L]
But with no luck ;)
Also is there a way to make everything Ive posted in less rules/conditions? I am going to host "some" sites and Im wondering how many rule check in httpd for each request is too much ;).
Thank you for your time.
Regards,
Vermon
I have been all the time trying to make it run ... and it seems I managed to do it
RewriteCond %{THE_REQUEST} ^[^/]+/index\.(html如hp) [nc]
RewriteRule ^/index\.(html如hp)$ [%{HTTP_HOST}...] [R=301,L]
But to be honest when looking at the code I dont know how is it working ;].
Its rewriting subdomain.example.com/index?id=3 -> subdomain.example.com/?id=3 and etc ( same with root domain, without parameters and etc ) ... strange ;).
If there is any other option ( better performance/SE ranking wise ) that ones I have used, I would be grateful for pointing them. Also do 5-6 condition checks/rewrite on each http request lower performance significantly?
Regards,
Vermon
RewriteCond %{THE_REQUEST} ^([^/]+/)+index\.(html如hp)[?\ ] [NC]
RewriteRule ^/([^/]+/)*index\.(html如hp)$ http://%{HTTP_HOST}/$1 [R=301,L]
As for using fewer rules, I wouldn't worry about it for two reasons. First, some sites do fine with over 500 RewriteRules per request in .htaccess files. And second, you are putting your code into httpd.conf, which means it is compiled into native code at server restart. This means it runs much faster than code in .htaccess, which is interpreted on every HTTP request. And mod_rewrite runs just as fast or faster than, say, a PERL or PHP script, and there are many sites using very large, complex scripts. For 50 rewriterules or fewer, you probably won't even notice anything, even on a very busy site.
This is not to say that every rule should not be optimized -- The better-optimized they are, the more rules you can add without affecting the server response time, and the longer it will be before you'll need to upgrade to a higher-performance server. But many sites have optimized rules using very un-optimized regular-expressions patterns, so watch out for that trap. This is my favorite 'anti-multiple- ".*" -pattern rant... :)
Jim
I had to make two changes :
First : ¦ instead of . in (php.html) - it was not matching index files without this change ( dot should be also treated as OR? )
Second : I used [^\@] instad of [^/] at rewrite cond - Its because lack of my knowledge ( how to match all characters ;) - probably .* or something like this ) - without it www.example.com/xyz/zyx/index.html was being rewritten to www.example.com/zyx/ instead of www.example/com/xyz/zyx/
I hope I didnt mess something other when making this changes ;).
Code after changes :
RewriteCond %{THE_REQUEST} ^([^/]+/)+index\.(html¦php)[?\ ] [NC]
RewriteRule ^/([^\@]+/)*index\.(html¦php)$ [%{HTTP_HOST}...] [R=301,L]
Also it seems to be working without [?\ ] - what is it for?.
Regards,
Vermon
So I cannot be sure about the changes you made -- I don't know if I see them as you typed them.
Nevertheless, the RewriteRule pattern "/([^/]+/)*" (hopefully showing as slash, left parenthese, left square bracket, carat, slash, right square bracket, plus, slash, right parenthese, asterisk) means "match a slash, followed by any number --including zero-- of the sub-pattern (one or more characters NOT equal to a slash, followed by a slash). This allows the rule to be applied to index.xyz files in any directory or subdirectory.
However, there is an error, in that the whole thing needs another layer of parentheses around it, which I forgot:
RewriteRule ^/(([^/]+/)*)index\.(html如hp)$ http://%{HTTP_HOST}/$1 [R=301,L]
The "[?\ ]" just makes the pattern more specific; It will reject "phps" or "htmlx", but accept "php?query=foo", for example. The intent is to serve as an effective "soft" end-anchor to the filetype.
Jim
Be aware that code matched for www.example.com and wwwexample.com only.
If you had intended to also match for example.com then you would have needed this:
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com [NC]
As you already know, that code had other problems, but I just wanted to clarify what the brackets and question mark do for you.