子域重定向使用htaccess

时间:2014-09-15 08:06:02

标签: php apache .htaccess mod-rewrite url-rewriting

我正在开发一个需要重写像

这样的网址的项目
  

https://orange.url.com

  

https://www.url.com/?s=orange

我需要协助如何制作htaccess来处理这项任务。 PS:请注意,我已经在我的htaccess中处理了以下重写事项。

RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]

# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

1 个答案:

答案 0 :(得分:0)

您可以将此代码放入htaccess(必须位于根文件夹中)

Options -MultiViews

RewriteEngine On
RewriteBase /

# To internally forward http://xxx.domain.com to http://www.domain.com/?s=xxx
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^$ index.php?s=%1 [L]

# To internally forward http://xxx.domain.com/yyy/ to http://www.domain.com/?s=xxx&c=yyy
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/$ index.php?s=%1&c=$1 [L]

# To internally forward http://xxx.domain.com/yyy/zzz/ to http://www.domain.com/?s=xxx&c=yyy&g=zzz
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?s=%1&c=$1&g=$2 [L]

# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} \s/(.+?)\.php [NC]
RewriteRule ^ %1/ [R,L]

# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R,L]

# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/$ $1.php [L]