使用Mod_Rewrite将ROOT重写为另一个路径

lee*_*eek 15 apache .htaccess mod-rewrite url-rewriting

注意:我已经尝试了这个问题下的建议,但它们要么不起作用,要么给我一个无限重定向循环错误.

我的.htaccess文件中有以下内容:

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    # Rewrite old links to new
    Redirect 301 /howitworks.php        /how-it-works
    Redirect 301 /testimonials.php      /testimonials
    Redirect 301 /contact_us.php        /customer-service
    Redirect 301 /affiliates.php        /affiliates
    Redirect 301 /account.php           /customer/account
    Redirect 301 /shopping_cart.php     /checkout/cart
    Redirect 301 /products.php          /starter-kits
    Redirect 301 /login.php             /customer/account/login

    # Force to Admin if trying to access admin
    RewriteCond %{HTTP_HOST} www\.example\.com [NC]
    RewriteCond %{REQUEST_URI} admin [NC]
    RewriteRule ^(.*)$ https://admin.example.com/index.php/admin [R=301,L,QSA]

    # Compress, Combine and Cache Javascript/CSS
    RewriteRule ^(index.php/)?minify/([^/]+)(/.*.(js|css))$ lib/minify/m.php?f=$3&d=$2

    # Always send 404 on missing files in these folders
    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

    # Never rewrite for existing files, directories and links
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI} !server-status

    # Rewrite everything else to index.php
    RewriteRule .* index.php [L]

    # Force www in url and non-example domains to example
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^ww2\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^qa\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^uat\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^local\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^admin\.example\.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]

</IfModule>
Run Code Online (Sandbox Code Playgroud)

我需要重写,它将执行以下操作:

重写:http ://subdomain.example.com/ http://subdomain.example.com/page

我也希望这个隐藏/页面(/将表现为/页面),但这不是必要的要求.此外,我希望它可以使用HTTP或HTTPS.

非常感谢任何帮助,因为我一直在努力解决这个问题.

sbe*_*der 22

正确的根路径重定向很简单:

RewriteEngine On
RewriteRule ^/$ /page [R]
Run Code Online (Sandbox Code Playgroud)

仅在一个子域上强制执行此操作(如果您的重写块在虚拟主机定义中,则几乎不会发生这种情况):

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^/$ /page [R]
Run Code Online (Sandbox Code Playgroud)

  • 这个答案根本不适用于htaccess上下文,并且完全重定向到客户端(IOW没有按照要求"隐藏") (5认同)
  • 一旦我使用^/$而不是^ $,这只适用于apache2.答案是错误的还是我犯了错误? (4认同)

cov*_*ner 5

复制已删除的回复:

在你的文档的htaccess中简单地说:

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^$ /page
Run Code Online (Sandbox Code Playgroud)

  • 不知道为什么这会被删除...... `RewriteRule ^$ /page` 似乎是唯一有效的答案。`RewriteRule ^/$ /page` _NOT_ 将请求匹配到站点的文档根目录。我正在修改当前接受的答案,使其正确。 (3认同)