我想将一些URL重定向到另一个PORT。我的.htaccess是:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteCond %{REQUEST_URI} !^(.*)(\.)(.*)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
我需要添加一条规则,该规则将所有开头带有^ some-prefix/的请求重定向到端口8080,例如:
1-网址
http://www.mysite.com/page1
将重定向到
http://www.mysite.com/page1/
但
2-网址
http://www.mysite.com/some-prefix/page2
将重定向到
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]
关于apache - .htaccess到其他端口的特定URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027847/