gpt4 book ai didi

.htaccess - mod_rewrite 将子域中的破折号替换为下划线,然后重定向

转载 作者:行者123 更新时间:2023-12-02 21:15:59 25 4
gpt4 key购买 nike

这是我想做的:

  • 我想将所有破折号 (-) 替换为下划线 (_),但仅限于子域中。
  • 替换所有破折号后,我想重定向到具有重写子域名称的子目录

例如: http://subdomain-with-dashes.rotarytest.de/a-directory/an-image.png重写后应该是 http://rotarytest.de/subdomain_with_dashes/a-directory/an-image.png

这是我现在所拥有的,请参阅代码中的注释

RewriteEngine on

# replace dashes with underscores
# this works, but only for the last dash
RewriteCond %{HTTP_HOST} ^(.*)-(.*)$
RewriteRule ^(.*)$ http://%1_%2/$1 [L,R=301]


# if a subdomain is called, redirect to subdirectory
# this code works but only when i have one dash in my subdomain
RewriteCond %{HTTP_HOST} ^(.*)\.rotarytest\.de$
RewriteRule ^(.*)$ http://rotarytest.de/%1%{REQUEST_URI} [L,R=301]

我尝试了在 stackoverflow 或网络上找到的几乎所有解决方案,但没有一个能正常工作。

有人可以帮我吗?预先感谢您。

最佳答案

您必须允许浏览器不断重定向才能删除破折号。如果您不需要将 URI 中的破折号替换为下划线,则您不需要前两条规则,它们不会应用于主机名。将前两条规则替换为:

RewriteCond %{HTTP_HOST} ^(.*)-(.*)$
RewriteRule ^(.*)$ http://%1_%2/$1 [L,R=301]

关于.htaccess - mod_rewrite 将子域中的破折号替换为下划线,然后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15427583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com