【发布时间】:2021-10-29 23:13:08
【问题描述】:
在本例中,我管理 domain.com。在其中,我在 php 中有一个商店模板,可以动态加载所选商店:
domain.com/store1domain.com/store2domain.com/store3
下面的.htaccess 正在生成漂亮的网址。后面真正加载的是这样的:
domain.com/store/index.php?store=store1domain.com/store/index.php?store=store2domain.com/store/index.php?store=store3
每个商店都有内部链接,例如:
domain.com/store1/cataloguedomain.com/store1/catalogue/instrumentsdomain.com/store1/catalogue/instruments/guitarsdomain.com/store1/electric-guitar/1337
它完全按预期工作。这些是在 domain.com 中使用的 .htaccess 规则。每个RewriteRule 中的store 查询字符串(store1、store2、store3)确定正在加载哪个商店:
# permalinks
RewriteEngine on
# errors
ErrorDocument 404 /error.php?error=404
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
#################################################
# store
RewriteRule ^([0-9a-zA-Z-]{2,50})/?$ store/index.php?store=$1 [QSA,L]
# store: catalogue
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/?$ store/catalogue.php?store=$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=$1&cat=$2 [QSA,L]
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/([0-9a-zA-Z-]{1,75})/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=$1&cat=$2&subcat=$3 [QSA,L]
# store: products
RewriteRule ^([0-9a-zA-Z-]{2,50})/([0-9a-zA-Z-]{1,150})/([0-9]+)$ store/product.php?store=$1&slug=$2&id_product=$3 [QSA,L]
现在,棘手的部分来了。一些客户想要使用自己的域,以便store1.com/* 加载与domain.com/store1/* 相同的内容(不使用重定向)。
例子:
store1.com=>domain.com/store1/store1.com/catalogue=>domain.com/store1/cataloguestore1.com/catalogue/instruments=>domain.com/store1/catalogue/instrumentsstore1.com/catalogue/instruments/guitars=>domain.com/store1/catalogue/instruments/guitarsstore1.com/electric-guitar/1337=>domain.com/store1/electric-guitar/1337
你明白了。
所有域(domain.com、store1.com、store2.com、store3.com)都配置在同一个 Apache Web Server 环境中(使用虚拟主机)。
问题是:这可以在每个商店的域根路径中的.htaccess 文件中动态实现还是在虚拟主机.conf 文件中实现?如果有,怎么做?
【问题讨论】:
-
这很可能是可行的,但具体的实现细节可能会有所不同。域名和商店名称之间的关系是什么(总是只是附加.com,如果是.co.uk,如果在store1.com之前有www.,如果有未知域的请求到达怎么办.. .)。此外,如果您有权访问 apache 配置 (.conf) 文件,则根本不应该使用 .htaccess。最后,您最好只在 Web 应用代码中处理所有这些逻辑。
-
@DusanBajic 感谢您的反馈。他们永远不会使用 www.。域类型可以是任何类型:
.com、.co.uk、.ec等。可能无法从 domain.com 根目录切换.htaccess逻辑,因为一切都在那里,解决方案应该-也许-在其他域的.conf文件中(?)关于从哪里开始寻找的任何想法? -
我假设域名,即。
store1instore1.com将是storeURL 参数的必需值,就像现在的“虚拟目录”/store1/一样? -
@MrWhite 没错。如果没有
store=store1值,domain.com中的模板将不知道要加载哪个存储。
标签: apache .htaccess mod-rewrite