.htaccess到不同端口的特定URL

let*_*cia 7 apache .htaccess mod-rewrite redirect

我想将一些URL重定向到另一个PORT.我的.htaccess是:

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

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

1- URL

http://www.mysite.com/page1
Run Code Online (Sandbox Code Playgroud)

将重定向到

http://www.mysite.com/page1/
Run Code Online (Sandbox Code Playgroud)

2- URL

http://www.mysite.com/some-prefix/page2
Run Code Online (Sandbox Code Playgroud)

将重定向到

http://www.mysite.com:8080/some-prefix/page2/
Run Code Online (Sandbox Code Playgroud)

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

Jus*_*man 10

你可以这样做

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]
Run Code Online (Sandbox Code Playgroud)

编辑:考虑您的评论的新代码

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]
Run Code Online (Sandbox Code Playgroud)

  • 当然。您可以看到我编辑过的答案(我为此添加了新代码) (2认同)