简体   繁体   English

.htaccess用子域AND目录重写为变量

[英].htaccess rewrite with subdomain AND directories to variables

Trying to get something like this to work but only the first scenario works. 试图使类似的东西起作用,但只有第一种情况起作用。 Basically I have three variables I need to pass, and these are the three scenarios: 基本上,我需要传递三个变量,这是三个方案:

1 1个

cat.domain.com should go to: index.php?pet=cat cat.domain.com 应该转到: index.php?pet = cat

2 2

cat.domain.com/orange.html should go to: index.php?pet=cat&color=orange cat.domain.com/orange.html 应该转到: index.php?pet = cat&color = orange

3 3

cat.domain.com/orange/fluffy.html should go to: index.php?pet=cat&color=orange&name=fluffy cat.domain.com/orange/fluffy.html 应该转到: index.php?pet = cat&color = orange&name = fluffy

I can only get the first to work with: 我只能第一个使用:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ index.php?pet=%2 [L]

But when I try this for #2 it doesn't seem to work or detect the "color" 但是当我尝试#2时,它似乎无法工作或无法检测到“颜色”

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com/([a-zA-Z0-9_-]+).html$ [NC]
RewriteRule ^$ index.php?pet=%2&color=%3 [L]

How would I write #2 and #3? 我将如何写#2和#3? Please help! 请帮忙!

URI data is not part of the HTTP Host field , therefore, you can't match against it in a %{HTTP_HOST} variable. URI数据不是HTTP Host字段的一部分 ,因此,您无法在%{HTTP_HOST}变量中将其与之匹配。 Use the pattern in the rule to match against that: 使用规则中的模式与之匹配:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ index.php?pet=%2 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)\.html$ index.php?pet=%2&color=$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([/]+)\.html$ index.php?pet=%2&color=$1&name=$2 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM