.htaccess特定的URL到不同的端口

我想redirect一些URL到不同的端口。 我的.htaccess是:

RewriteEngine on RewriteCond %{REQUEST_URI} !^(.*)/$ RewriteCond %{REQUEST_URI} !^(.*)(\.)(.*)$ RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L] 

我需要添加一个规则,使用^ some-prefix /将所有请求redirect到端口8080,例如:

1- URL

 http://www.mysite.com/page1 

将redirect到

 http://www.mysite.com/page1/ 

2- URL

 http://www.mysite.com/some-prefix/page2 

将redirect到

 http://www.mysite.com:8080/some-prefix/page2/ 

我怎样才能做到这一点? 谢谢

你可以这样做

 RewriteEngine on # redirect to 8080 if current port is not 8080 and "some-prefix/" is matched RewriteRule ^some-prefix/(.*[^/])/?$ http://www.mysite.com:8080/some-prefix/$1/ [R=301,L] # redirect with trailing slash if not an existing file and no trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !/$ RewriteRule ^(.*)$ /$1/ [R=301,L] 

编辑:考虑到您的意见的新代码

 RewriteEngine on # redirect to 8080 if "some-prefix/" is matched RewriteCond %{SERVER_PORT} !^8080$ RewriteRule ^some-prefix/(.*[^/])/?$ http://%{HTTP_HOST}:8080/some-prefix/$1/ [R=301,L] # redirect with trailing slash if not an existing file and no trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !/$ RewriteRule ^(.*)$ /$1/ [R=301,L]