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 は正規表現をサポートしているため、次の置換を使用できます。

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

括弧は last まですべて一致し/ます。

次に、グループ参照を使用して再利用します$1。残りはドロップされます。

regexp の使用方法については、mod_rewrite のヘルプを確認してください。

于 2012-07-04T05:24:39.310 に答える