apache htaccess rewrite with alias

31,393

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.

Share:
31,393
edroms
Author by

edroms

Updated on December 03, 2020

Comments

  • edroms
    edroms over 3 years

    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.