.htaccess到不同端口的特定URL

时间:2014-04-12 08:00:52

标签: apache .htaccess mod-rewrite redirect

我想将一些网址重定向到其他PORT。我的.htaccess是:

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

我需要添加一条规则,将开头的^ some-prefix /重定向到端口8080,例如:

1- URL

http://www.mysite.com/page1

将重定向到

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

BUT

2- URL

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

将重定向到

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

我该怎么做?感谢

1 个答案:

答案 0 :(得分:9)

你可以这样做

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]