mod-rewrite去掉.htaccess中的子目录

时间:2012-07-04 05:21:29

标签: .htaccess mod-rewrite apache

如何重写URL,例如:

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

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

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

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

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

parens匹配最后/

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

检查mod_rewrite有关如何使用regexp的帮助。