【问题标题】:apache htaccess rewrite with aliasapache htaccess 用别名重写
【发布时间】:2013-12-18 12:09:23
【问题描述】:

我们正在更改我们的域名,这旨在适用于独立应用程序。在 Apache 虚拟主机文件中,DocumentRoot 是 /var/www/website/html,而不是本块中的 /var/www/example/html:

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>

我在 /var/www/example/html/apps/dept 目录下放了一个 .htaccess 文件,如下:

 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]

这似乎遵循这里的建议,http://httpd.apache.org/docs/current/mod/mod_rewrite.htmlApache : How to Use Rewrite Engine Inside Alias。我看不到任何结果。新域在 VH 文件中也有一个虚拟主机配置。同样的基本重写适用于我们不使用别名的 Drupal 网站。使用附加的应用程序路径名重写域名可能需要进行哪些更改? RewriteBase 不正确吗?

谢谢。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    所以你只想重定向/apps/dept/,对吗?这应该有效。将其作为.htaccess 或在example.orgname.state.tx.us 的Apache 配置中放置,一切都应该按预期工作。

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

    所以现在,任何请求都会转到此 URL

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

    现在将转到此 URL:

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

    并且URL右侧的任何请求参数也将被传递。

    编辑只需重读您在此处写的内容:

    我在 /var/www/example/html/apps/dept 目录中放置了一个 .htaccess 文件 如下。

    我上面描述的.htaccess应该放在/var/www/example/html/而不是/apps/dept子目录中。

    但是,如果您希望 .htaccess 中的 /apps/dept 具有相同的行为,请使用以下命令:

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

    这样,从/apps/dept 发出的任何请求都将发送到example.orgname.texas.gov/apps/dept/,包括/apps/dept 的子目录,例如/apps/dept/test_app1/apps/dept/test_app2/apps/dept/test_app3

    或者试试这个:

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

    请注意,我删除了 ^,这将强制 RewriteRule 匹配 URL 的开头。

    【讨论】:

    • 我想重定向 /apps/dept/ 目录下的应用程序。
    • 当你的意思是“下面”你是什么意思?它将从http://example.orgname.state.tx.us/apps/dept/ 重定向到http://example.orgname.texas.gov/apps/dept/,包括该目录中包含的目录。如果我有误解,请澄清问题。
    • 希望包括这些目录:example.on.state.tx.us/apps/dept/myappexample.on.texas.gov/apps/dept/myapp。在 dept dir 中大约有 10 个应用程序。
    • 是的,我所描述的重定向会将/apps/dept/ 及以下的任何内容(例如/apps/dept/myapp)发送到新主机名。该 URL 右侧的任何内容 100% 都将被正确重写和重定向。查看我的编辑以了解。
    • 你是对的。此解决方案有效,此问题已解决。谢谢。
    【解决方案2】:

    您无法匹配 %{HTTP_HOST} 变量中的请求 URI,它仅匹配域名。将您的规则更改为:

     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]
    

    【讨论】:

    • 这仅适用于 /apps/dept/ 目录,但不适用于 dept 下的目录。有没有办法在不为每个应用程序执行和 .htaccess 的情况下处理部门下的所有应用程序目录?
    猜你喜欢
    • 2021-03-15
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多