gpt4 book ai didi

.htaccess - mod_rewrite 为域选择一个文件夹

转载 作者:行者123 更新时间:2023-12-03 06:51:07 25 4
gpt4 key购买 nike

情况

我正在使用 Zend 框架,因此尝试通过地址调用文件夹以失败告终,我们收到错误:“指定的 Controller 无效”。我需要将附加论坛连接到单独文件夹中的应用程序。我按如下方式更改 .htaccess 文件:

RewriteRule ^forum(.*)$ forum$1 [L]

有一段时间它很好,直到我意识到该页面存在于两个域中的两个版本(php 识别域 [.pl/.co.uk] 并选择语言)。因此,我决定将论坛分成两个单独的模块(例如,波兰语版本论坛的文件夹为:forum_pl,英国版本的文件夹为:forum_uk - 两个不同的论坛)

问题是什么?

它需要重写 .htaccess 文件才能像这样工作:

如果: www.domena.pl/forum

然后:从文件夹/forum_pl 打开脚本

如果: www.domain.co.uk/forum

然后:从文件夹/forum_uk 打开脚本。

最糟糕的是,当我尝试添加 RewriteCond 来修复 .htaccess 文件时,即使返回到原始(如上所示)版本之后,第一个重定向也不再正常工作。

如果我没有找到之前已经给出的答案,请帮助我。

编辑:

第一个可行的解决方案

RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]

看起来它想要工作,但是需要解决的问题是(TODO):

  1. 当您输入:www.domain.pl/forum 时,您将访问 www.domain.pl/forum_pl/,但如果您输入:www.domain.pl/forum/,您将访问 www.domain.pl/forum/(从forum_pl读取)。如何使其在第一种情况下与第二种情况下一样工作。

编辑:

我最后的解决方案:

#FORUM
#PL
RewriteCond %{REQUEST_URI} ^/forum$ [NC]
RewriteRule ^(.*)$ forum/ [R=301,L]

RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]

RewriteCond %{REQUEST_URI} ^/forum_pl(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domena.pl/forum/ [R=301,L] #Dosen't work properly (?)

RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_pl/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_pl$1 [L]
#UK
RewriteCond %{REQUEST_URI} ^/forum_uk(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/forum/ [R=301,L] #Dosen't work properly (?)

RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_uk/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_uk$1 [L]

最终工作版本:

RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]

RewriteRule ^forum_pl(.*)$ http://www.domena.pl/forum$1 [R=301,L] # Not working!
RewriteRule ^forum_uk(.*)$ http://www.domain.co.uk/forum$1 [R=301,L] # Not working!

# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]

# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]

# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]

遗憾的是,您可以从浏览器中手动调用forum_uk和forum_pl文件夹。

最佳答案

不确定我在你的长问题中是否正确定位并进行了一些演变:-)让我们试试这个:

# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]

# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum/(.*)$ http://www.domena.pl/forum_pl/$1 [R=301,L]

# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum/(.*)$ http://www.domain.co.uk/forum_uk/$1 [R=301,L]

如果您需要重写以采取不同的方式,请告诉我

关于.htaccess - mod_rewrite 为域选择一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13272679/

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