Forum Moderators: phranque
You helped me with mod rewrite about a year ago and now, after I migrated to a dedicated server none of my rules work anymore :(
I tried to follow your examples in numerous posts but when I change anything it either doesn't work or my JSPs not compiling properly and as a result they act as html pages...
1) I do not use my main domain and need to redirect everything to http://www.subdom.example.com (which is my main site)
2) Anything from both example.com and subdom.example.com I need to redirect to http://www. in case somebody types subdom.example.com or www.subdom.example.com (same for example.com)
3) And there are a few redirects on URLs, like articles, calculator, engine, etc.
4) I have an additional subdomain (say subdom2.example.com) and I'd like to have similar rules for it as in # 2 and #3
Here is my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdom\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.subdom\.example\.com$ [NC]
RewriteRule ^subdom/articles[/]?$ http://www.subdom.example.com/articles.jsp [R=301,L]
RewriteRule ^subdom/calculator[/]?$ http://www.subdom.example.com/calculator.jsp [R=301,L]
RewriteRule ^subdom/engine[/]?$ http://www.subdom.example.com/calculator.jsp [R=301,L]
RewriteCond %{HTTP_HOST} ^(subdom\.)?example\.com [NC]
RewriteRule ^([^/]*/)+(.*)$ http://www.subdom.example.com/$2 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www.subdom.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://www.subdom.example.com/ [R=301,L]
I really, really need your help!
THANKS!
Axel
[edited by: jdMorgan at 5:03 pm (utc) on April 10, 2008]
[edit reason] example.com [/edit]
If you plan to move your rules into the server config (which would be a clever move resourcewise), then you need to slightly change them:
RewriteRule index.html -will match in .htaccess, while
RewriteRule /index.html -will match in the server config.
By the way, is mod_rewrite enabled at all? :-)
But I'll give it a shoot anyway:
If you want to redirect all requests which are not for www.subdom.example.com, then you can do the following:
RewriteCond %{HTTP_HOST} !^www\.subdom\.example\.com [NC]
RewriteRule ^(subdom/)?(.*)$ http://www.subdom.example.com/$2 [R=301,L] This will redirect all request (with the path information preserved) to http://www.subdom.example.com/. There is no indication on what's the purpose of the
RewriteRule ^([^/]*/)+(.*)$ ...directive, so I made a guess, it was meant to remove the possible subdom directory from the path?.
After these hostname based redirect you can do the directory based ones, the calculator and the others.
So if in understand your explanation well, the following should do what you are looking for (without knowing your link structure - to repeat myself):
RewriteEngine on
#
# Redirect when the request is not for www.subdom.example.com
RewriteCond %{HTTP_HOST} !^www\.subdom\.example\.com [NC]
RewriteRule ^(subdom/)?(.*)$ http://www.subdom.example.com/$2 [R=301,L]
#
# Do the directory based redirects (at this point the hostname must be
# the proper one already, so we don't need "subdom" anymore.
RewriteRule ^articles/?$ http://www.subdom.example.com/articles.jsp [R=301,L]
RewriteRule ^calculator/?$ http://www.subdom.example.com/calculator.jsp [R=301,L]
RewriteRule ^engine/?$ http://www.subdom.example.com/calculator.jsp [R=301,L]
And at last, not connected to rewriting directly, but Tomcat. If you are using mod_jk to connect to Tomcat from Apache, then you may end up with problems compiling jsp files, when the tomcat could not find the context for the given url. This also depends on mod_jk directives like JKMount, but also depends on the Tomcat configuration. So be warned using mod_jk and mod_rewrite together is not the best thing to do.
[edited by: jdMorgan at 5:06 pm (utc) on April 10, 2008]
[edit reason] example.com [/edit]
In order to avoid multiple "chained" redirects, it is best to put your redirects in order from most-specific (.e.g. per-page redirects) to least-specific (hostname canonicalization redirects). That way, if a page or directory is redirected, the hostname is also canonicalized in the same step, rather that requiring a second redirect on the subsequent HTTP request.
Following the external redirects, put your internal rewrites, again in order from most-specific to least-specific. Putting the internal rewrites after the external redirects prevents the problem of having external redirects 'expose' internally-rewritten URL-paths to the client.
Jim
Sorry for such late reply - I was so busy with my dedicated server "nightmare" and just got some time to look into rewrites.
I tried to test rewrite with simple example (just to see if it works) and I got no effect! I wanted to see if any hit to example.com or www.example.com will be redirected to http://www.example.com
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
I checked mod_rewrite module by executing httpd -l and httpd -M, in both cases it is listed. I have Apache 2.2.6 and when I add line LoadModule rewrite_module modules/mod_rewrite.so to httpd.con I get the following error: httpd: Syntax error on line 34 of /usr/local/apache/conf/httpd.conf: module rewrite_module is built-in and can't be loaded
So I assume it is installed. I think I was not clear with my previous post, sorry about that. Here are the things that I'm trying to achieve on .htaccess level first:
1) I have multiple subdomains. In my previous example subdom.example.com happened to be the "main" subdomain. Any requests to example.com (main domain) need to be redirected to http://www.subdom.example.com.
So, any request to example.com -> http://www.subdom.example.com
2) Any requests to a particular subdomain (I have 3 of them) without http or www should redirect to http://www.subdom#.mysie.com (ie, http://www.subdom1.example.com, http://www.subdom2.example.com, or http://www.subdom3.example.com)
3) And finally (or as Jim said the most detailed should be first...) specific redirects for particular words (like articles, engine, calculator, etc.) to particular pages on each subdomain. These words can be different for each subdomain.
A little help over here, please?
[edited by: jdMorgan at 5:08 pm (utc) on April 10, 2008]
[edit reason] example.com [/edit]
When fighting, take on one opponent at a time if possible... not the whole gang all at once!
If you want an even simpler rule for initial .htaccess testing, then use something like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule .* http://www.google.com/ [R=301,L]
If you've got that and the rule above doesn't work, it might be a good time to find out how good the support is for your new host. :)
Jim
the actual physical structure of directories have changed. On new server I have ROOT folder added to each subdomain (Tomcat did not want to compile includes util we created this structure), for example:
/public_html/ROOT/
/public_html/ROOT/subdom/ROOT/
/public_html/ROOT/subdom1/ROOT/
/public_html/ROOT/subdom2/ROOT/
etc.
But from the URLs stand point, nothing have changed.
In httpd.conf I have this block
<Directory "/">
Options All
AllowOverride All
</Directory>
<Directory "/usr/local/apache/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
and than again for a some reason I see
<Directory "/">
Options All
AllowOverride All
</Directory>
Is this what you were saying about allowing to use .htaccess?
I'm not familiar with above directive...
I mentioned flushing your browser cache above, not your DNS cache. I meant that you should use
"Tools->Internet Options->General->Browsing History->Delete->Temporary Internet Files->Delete" in IE, or use
"Tools->Options->Advanced->Network->Cache->Clear Now" in Firefox.
When testing, you must flush your browser cache before testing any new server-side code.
If you do not flush your cache, then there is no guarantee that your browser will send a request to your server; It may simply show you a previously-cached page or response, and never send a request to your server. If it does not send a request to your server, then your server-side code will not execute, and cannot affect what you see in your browser.
Jim