如何重写需要同时捕获 REQUEST_URI 和 HTTP_HOST 的 URL?

如何重写需要同时捕获 REQUEST_URI 和 HTTP_HOST 的 URL?

我需要重写 REQUEST_URI 和 HTTP_HOST,但是在执行此操作时出现重定向错误:

# Capture mysite.com/pages/123
RewriteCond %{REQUEST_URI} ^/pages/[0-9]+/?$
RewriteRule ^/pages/([0-9]+)/?$ /home/mysite/www/pages.php?id=$1
# Now capture the subdomain
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).mysite.com
RewriteRule (.*) $1&subdomain=%1 [L,QSA]

我在这里做错了什么?

谢谢

答案1

我会尝试以下类似的事情;

# This will map requests like 
# http://XXX.mysite.com/pages/123?somevar=someval&more=more2
# to /dir/pages.php?id=123&subdomain=XXX&somevar=someval&more=more2
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).mysite.com
RewriteRule ^/pages/([0-9]+)$ /dir/pages.php?id=$1&subdomain=%1 [L,QSA]

第一个 RewriteCond%{REQUEST_URI} 可能没有必要,因为它包含所请求 URI 的路径组件,该组件在 RewriteRule Pattern 匹配中已经可用。

相关内容