Forum Moderators: phranque

Message Too Old, No Replies

Need help with mod rewrite !

         

mnemonik23

3:02 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Hi Jim,

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]

Samizdata

3:55 pm on Mar 5, 2008 (gmt 0)

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



I migrated to a dedicated server

In that case I would have thought that setting directives in httpd.conf was a better option.

But then my name is not Jim...

mnemonik23

4:03 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



I'm open to any suggestions since this is my first dedicated server.
But I assume if these did not work in .htaccess they will not work in httpd.conf, right?

gergoe

5:09 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



If you rules were working before, and they are not working after, then you might be struck by the simple problem that the parsing of .htaccess files are not enabled? See the AllowOverride [httpd.apache.org] directive on how to enable .htaccess files. Don't forget that you have to enclose it in a <Directory> container.

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:

  • You don't need Options +FollowSymLinks anymore
  • RewriteRules should start with a slash. While in .htaccess context the parent directories are stripped form the input url, but in the server config they are preserved. For example
    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? :-)

gergoe

5:41 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Besides, I tried figure out what you wanted to achieve with your directives (and match them against your explanation), but by understanding skill is like my explanation skill it seems - rather limited :-)

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]

jdMorgan

2:52 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> After these hostname based redirect you can do the directory based ones, the calculator and the others.

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

mnemonik23

6:08 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



gergoe / 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]

jdMorgan

6:49 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's be direct, shall we? If the code worked on your old server and does not work on your new server, and the URL-structure of the new s4erver is the same, then don't change anything in the code until you find out why the new server won't even run it. It is a waste of time moving the rules to httpd.conf or making modifications to the rules themselves until the old code works again.

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]

You will need to set AllowOverride Options -at least- in httpd.conf to use "Options" in .htaccess.

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

mnemonik23

7:09 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



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>

before module IfModule mod_security.c

Is this what you were saying about allowing to use .htaccess?
I'm not familiar with above directive...

mnemonik23

5:35 pm on Apr 2, 2008 (gmt 0)

10+ Year Member



Tried


Options +FollowSymlinks
RewriteEngine on
RewriteRule .* http://www.google.com/ [R=301,L]

Still see my site but all JSPs are "broken".

jdMorgan

8:46 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would mean you forgot to flush your browser cache then, because that rule should redirect everything on your site to google.com...

Jim

mnemonik23

4:07 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Jim,

I tried to flush dns but still I see my site with broken JSPs, I mean images are broken (not displayed), css style do not work, very messy...

Thanks,
Lidiya

[edited by: jdMorgan at 5:10 pm (utc) on April 10, 2008]
[edit reason] tidied-up [/edit]

mnemonik23

11:15 am on Apr 15, 2008 (gmt 0)

10+ Year Member



What should I do?

Maybe this is my problem?


RewriteMap LeechProtect prg:/usr/local/cpanel/bin/leechprotect
RewriteLock /usr/local/apache/logs/rewrite_lock

I just have no idea what to do...

jdMorgan

4:43 pm on Apr 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We're suffering here from an apparently-complicated problem, and a dearth of information. That, combined with the long "response time" in a forum makes this project a poor candidate for a forum-based solution. If I were you, I'd be looking for local help -- someone (from a nearby university, perhaps?) who can actually look over yous shoulder and make troubleshooting suggestions in real-time.

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