3

We are changing our domain name and this is meant to work for stand alone applications. In Apache virtual host file the DocumentRoot is /var/www/website/html, not /var/www/example/html as in this block:

Alias /apps/dept /var/www/example/html/apps/dept
<Directory "/var/www/example/html/apps/dept/">
   Options FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

I put an .htaccess file in /var/www/example/html/apps/dept directory as follows:

 RewriteEngine On
 RewriteBase /apps/dept/
 RewriteCond %{HTTP_HOST} ^example.orgname.state.tx.us/apps/dept [NC]
 RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]

This seems to follow what is recommended here, http://httpd.apache.org/docs/current/mod/mod_rewrite.html and Apache : How to Use Rewrite Engine Inside Alias. I see no results. The new domain has a virutal host config in the VH file, also. This same basic rewrite works for our Drupal website which does not use an alias. What changes might be necessary to have the domain name rewritten with an appended application pathname? Is the RewriteBase incorrect?

Thx.

Community
  • 1
  • 1
edroms
  • 45
  • 1
  • 1
  • 8

2 Answers2

3

So you only want to redirect /apps/dept/, correct? This should work. Place it as an .htaccess or in the Apache config for example.orgname.state.tx.us and all should work as expected.

RewriteEngine on
RewriteRule ^/apps/dept/(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]

So now, any requests going to this URL

http://example.orgname.state.tx.us/apps/dept/

Will now go to this URL:

http://example.orgname.texas.gov/apps/dept/

And any request parameters to the right of the URL will be passed along as well.

EDIT Just reread what you wrote here:

I put an .htaccess file in /var/www/example/html/apps/dept directory as follows.

The .htaccess I described above should be placed in /var/www/example/html/ and not in the /apps/dept subdirectory.

But if you want the same behavior from an .htaccess placed in /apps/dept then use this:

RewriteEngine on
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]

This way any request made from /apps/dept will to to example.orgname.texas.gov/apps/dept/ including subdirectories of /apps/dept such as /apps/dept/test_app1, /apps/dept/test_app2 or /apps/dept/test_app3.

Or perhaps try this:

RewriteEngine on
RewriteRule (.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]

Note I removed the ^ which would force the RewriteRule to match the beginning of the URL.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • I'd like to redirect applications that are in directories below the /apps/dept/ directory. – edroms Dec 02 '13 at 17:49
  • When you mean “below” what do you mean? It will redirect anything from `http://example.orgname.state.tx.us/apps/dept/` to `http://example.orgname.texas.gov/apps/dept/` including directories contained in that directory. Please clarify question if I’m misunderstanding. – Giacomo1968 Dec 02 '13 at 18:17
  • Hope to include these directories: http://example.on.state.tx.us/apps/dept/myapp/ to http://example.on.texas.gov/apps/dept/myapp. There are about 10 applications in the dept dir. – edroms Dec 02 '13 at 18:22
  • Yes, the redirects as I describe will send anything from `/apps/dept/` and below that such as `/apps/dept/myapp` to the new hostname. 100% of anything to the right of that URL will be correctly rewritten & redirected. Look at my edits to understand. – Giacomo1968 Dec 02 '13 at 18:24
  • Thanks. I tried a copy/paste of the above .htaccess contents in the /apps/dept/ dir and this returned https://example.on.texas.gov/var/www/example/html/apps/dept/example.on.texas.gov/apps/dept/ in the URL box of the browser. Looks like the reverse alias is in it. So close. – edroms Dec 02 '13 at 19:05
  • My bad. I forgot to add the `http://` to the final URL in front of `example.orgname.texas.gov/apps/dept/` but it should be fixed now. Look at my edits & try again. – Giacomo1968 Dec 02 '13 at 19:23
  • It works for the /apps/dept/ directory but not for the apps in the subdirectories. – edroms Dec 02 '13 at 20:04
  • Perhaps the apps in the subdirectories have hardcoded paths? Who knows. Based on our original question, these redirects & rewrites should work. – Giacomo1968 Dec 02 '13 at 20:05
  • 1
    You are correct. This solution works and this issue is resolved. Thanks. – edroms Dec 02 '13 at 20:50
  • Solution runs all apache requests for host files within path through the redirect rule. Is there a performance hit in this, rather than sending only orgnm\.tx\.us requests through the rule? There is VH config for the new domain, orgnm.texas.gov, so no need to catch those. Just a hunch. – edroms Dec 03 '13 at 18:51
  • “Is there a performance hit in this…” Never had an issue with this when I have done it for sites with hundreds if not thousands of links. If there is a performance hit, it’s negligible at best. – Giacomo1968 Dec 03 '13 at 18:55
0

You cannot match Request URI in %{HTTP_HOST} variable, it matches only domain name. Chang your rule to:

 RewriteEngine On
 RewriteBase /apps/dept/

 RewriteCond %{HTTP_HOST} ^example\.orgname\.state\.tx\.us$ [NC]
 RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This works for the /apps/dept/ directory only, but not for directories under dept. Is there a way to take care of all the app dirs under the dept without doing and .htaccess for each application? – edroms Dec 02 '13 at 17:57