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

发布于 2024-08-31 06:09:09 字数 2416 浏览 2 评论 0原文

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

我的 .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>

我需要重写来执行以下操作:

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

我还希望隐藏 /page (/ 会表现得好像它是 /page),但这不是必要的要求。另外,我希望它能够与 HTTP 或 HTTPS 一起使用。

非常感谢任何帮助,因为我已经为此奋斗了几个小时。

Note: I have tried the suggestions under this question but they either don't work or give me an infinite redirect loop error.

I have the following in my .htaccess file:

<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>

I need a rewrite that will do the following:

Rewrite: http://subdomain.example.com/ to http://subdomain.example.com/page

I would also like this to hide the /page (/ would act as if it was /page) but this is not a necessary requirement. Also, I'd like it to work with HTTP or HTTPS.

Any help is greatly appreciated because I have been struggling with this for hours.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云之铃。 2024-09-07 06:09:09

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

RewriteEngine On
RewriteRule ^/$ /page [R]

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

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^/$ /page [R]

A correct redirect of the root path is simply :

RewriteEngine On
RewriteRule ^/$ /page [R]

to force this only on one subdomain (which should barely happen if your rewrite block is in a virtualhost definition) :

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^/$ /page [R]
初熏 2024-09-07 06:09:09

复制我删除的回复:

在您的文档根目录的 htaccess 中:

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^$ /page

Copying my deleted response:

In htaccess of your documentroot simply:

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^$ /page
飘逸的'云 2024-09-07 06:09:09

您有两种选择将根目录重写为另一个路径,

  1. DirectoryIndex:

    DirectoryIndex anotherPath
    
  2. 修改重写:

    RewriteEngine 开启
    RewriteRule ^/?$ /anotherPath [L]
    

您可以使用上述选项之一将 example.com/ 内部重定向到 example.com/anotherPath

参考文献:

  1. https://httpd.apache.org/docs/current/mod/ mod_dir.html
  2. http://httpd.apache.org/docs /current/mod/mod_rewrite.html

You have two options to rewrite your root to another path,

  1. DirectoryIndex:

    DirectoryIndex anotherPath
    
  2. Mod-rewrite:

    RewriteEngine on
    RewriteRule ^/?$ /anotherPath [L]
    

You can use one of the above options to internally redirect example.com/ to example.com/anotherPath.

References:

  1. https://httpd.apache.org/docs/current/mod/mod_dir.html
  2. http://httpd.apache.org/docs/current/mod/mod_rewrite.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文