Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess specific URL to different port

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

like image 741
leticia Avatar asked Apr 12 '14 08:04

leticia


1 Answers

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]
like image 113
Justin Iurman Avatar answered Oct 09 '22 21:10

Justin Iurman