0

我将如何重写 URL,例如:

http://www.site.com/sub-directory/page-name

http://www.site.com/page-name

?

4

2 回答 2

1

我相信您的预期解决方案是使用Virtual Hosts,但如果您无权访问 httpd.conf,您可以尝试以下重写规则:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} =host1.example.com
RewriteCond %{REQUEST_URI} !^/host1-dir
RewriteRule ^(.*) host1-dir/$1 [L]

RewriteCond %{HTTP_HOST} =host2.example.com
RewriteCond %{REQUEST_URI} !^/host2-dir
RewriteRule ^(.*) host2-dir/$1 [L]

RewriteCond %{HTTP_HOST} =host3.example.com
RewriteCond %{REQUEST_URI} !^/host3-dir
RewriteRule ^(.*) host3-dir/$1 [L]

# If doesn't match any of it then 404 Not Found or anything you like
RewriteCond %{HTTP_HOST} =host1.example.com
RewriteCond %{HTTP_HOST} =host2.example.com
RewriteCond %{HTTP_HOST} =host3.example.com
RewriteRule .* - [R=404,L]
于 2012-07-04T05:54:56.740 回答
0

IIRC mod_rewrite 支持正则表达式,因此您可以使用它的替换:

^(.*/)[^/]*& 

括号匹配到最后/

然后你使用组引用重新使用它,我认为它是$1. 其余的被丢弃。

查看关于如何使用正则表达式的 mod_rewrite 帮助。

于 2012-07-04T05:24:39.310 回答