apache - .htaccess到其他端口的特定URL

标签 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]

我需要添加一条规则,该规则将所有开头带有^ 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/

相关文章:

python - 使用 mod-python 使 HTTP 身份验证成为可选

apache - 引用:mod_rewrite,URL重写和“漂亮的链接”解释

.htaccess - HTACCESS 中带有字符 % 的 URL 重定向

regex - .htaccess 重定向 + 重写 : Prefix uri with language code while keeping initial uri

Apache:关于强制 www - 排除通用文件是否有意义?

java - Apache 2.4 + Glassfish Server 2.1 发布过大错误

java - 读取14位和16位图像

apache - 在 AWS 服务器上安装 Apache 后,我无法再 SSH

.htaccess - htaccess 将文件夹重定向到另一个文件夹

wordpress - 在 WordPress 中使用 .htaccess 删除尾部斜杠