【问题标题】:Rewrite page url as subdomain in apache htaccess在apache htaccess中将页面url重写为子域
【发布时间】:2023-04-01 12:32:01
【问题描述】:

我有很多页面 URL,例如 domain.com/page 我想要一个重写规则,它将我的所有页面 URL 更改为 page.domain.com,这意味着域之后会发生什么,只需将其重写为子域

示例网址

expertpro.cloud/hot-to-write-blog 到 hot-to-write-blog.expertpro.cloud

expertpro.cloud/game 到 game.expertpro.cloud

expertpro.cloud/nibm-full-form 到 nibm-full-form.expertpro.cloud

expertpro.cloud/choclate 到 choclate.expertpro.cloud

expertpro.cloud/harmony-in-life 到 Harmony-in-life.expertpro.cloud

expertpro.cloud/paki-cold-places 到 paki-cold-places.expertpro.cloud

expertpro.cloud/you-are-one 到 you-are-one.expertpro.cloud

我已经有一些 Nginx 的代码

             The empty location = / block is necessary so that you don't redirect 
               http://example.com/ to http://.example.com/.


             //replacing domain name in rewrite rule

           location = / {
           # don't redirect $server_name/
             }


          location / {
            rewrite ^\/([^\/]*).*$ https://$1.$server_name/ redirect;

【问题讨论】:

  • 您的问题含糊不清。请在精度上投入更多的精力。给出一些特定 URL 的示例以及如何重写它们。这是关于一个文件夹/“子域”还是多个?并且“基本域”根本不应该被使用?您的问题下方有一个“编辑”链接。 使用它...
  • @arkascha 已更新
  • @arkascha Expertpro.cloud/hot-to-write-blog 到 hot-to-write-blog.expertpro.cloud Expertpro.cloud/game 到 game.expertpro.cloud Expertpro.cloud/nibm-完整形式到 nibm-full-form.expertpro.cloud Expertpro.cloud/choclate 到 choclate.expertpro.cloud -paki-cold-places.expertpro.cloudexpertpro.cloud/you-are-one 到 you-are-one.expertpro.cloud 的地点

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


【解决方案1】:

好的,你所需要的只是内部重写,就像它看起来的那样:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]

您显然需要将重写模块加载到您的 http 服务器中。

这将是一种将直接请求另外重定向到内部 URL 的变体:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/?([^/]+)(/.*)?$ https://$1.example.com$2 [R=301]

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]

在这里,从 R=302 临时 重定向开始,并且只有在一切按预期工作后才将其更改为 R=301 永久 重定向是有意义的。

一般来说,您应该尝试在实际的 http 服务器的主机配置中实现这些规则。如果您无权访问,则可以改用分布式配置文件(“.htaccess”),但这些都有缺点。在这种情况下,您需要启用对此类文件的解释。

【讨论】:

  • 非常感谢用 [R=301,QSA,L] 替换这个 [R=301] 对我有用。 RewriteCond 上的 RewriteEngine %{HTTP_HOST} ^(www\.)?hostingwebsite\.io$ RewriteRule ^/?([^/]+)(/.*)?$ https://$1.hostingwebsite.io$2 [R= 301,QSA,L] RewriteCond %{REQUEST_URI} !^/wp-admin RewriteCond %{REQUEST_URI} !^/wp-login* RewriteCond %{REQUEST_URI} !^/sitemap* RewriteCond %{REQUEST_URI} !^/main-sitemap *
  • 好的,QSA 标志在需要时是有意义的,你的问题没有说明这样的事情。 L 标志没有意义,外部重定向总是会终止当前的重写过程。
猜你喜欢
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2019-12-05
  • 1970-01-01
相关资源
最近更新 更多