I want redirect some URLs to a different PORT. My .htaccess is:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteCond %{REQUEST_URI} !^(.*)(\.)(.*)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
I need to add a rule that redirects all requests with ^some-prefix/ at the beginning to port 8080, example:
1- URL
http://www.mysite.com/page1
will redirect to
http://www.mysite.com/page1/
BUT
2- URL
http://www.mysite.com/some-prefix/page2
will redirect to
http://www.mysite.com:8080/some-prefix/page2/
How can I do this? Thanks
You can do it this way
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]
EDIT: new code taking your comment into consideration
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With